From: Rick DeNatale on 14 Dec 2009 09:28 On Mon, Dec 14, 2009 at 4:59 AM, Bertram Scharpf <lists(a)bertram-scharpf.de> wrote: > Hi, > > Am Montag, 14. Dez 2009, 16:05:54 +0900 schrieb Ruby Newbee: >> Oh sorry I have found that. >> need to convert the arguments to an array. >> >> "Hello, %s %s" %(["Matz!","again"]) >> => "Hello, Matz! again" > > Be aware that % is interpreted as an operator because of the > string in front of it. There is also a shortcut > > %("Matz!","again") > > for > > %Q("Matz!","again") > > which would be a string. Actually, I'm not sure how that % before the ( is being seen by the parser. By itself %("Matz!","again") => "\"Matz!\",\"again\"" For either Ruby 1.8.6 or 1.9. %( should interpret everything up to the the matching ) as part of a string including the "'s and the , I'm not sure why it doesn't do the same thing as: > "Hello, %s %s" % "\"Matz!\",\"again\"" ArgumentError: too few arguments from (irb):3:in `%' from (irb):3 Since the format string needs two substitutions and we are only giving it one. Perhaps a subtle Ruby parsing/lexing bug. > What you do is applying the mod(%) operator to a string: > > str % array > "%s %d %f" % [ "hi", 33, 0.618] #=> "hi 33 0.618000" No, this is sending the message % to the string. String#% is NOT mod, the documentation (informally calls it format) and directs you to Kernel#sprintf for further explanation. Other that sharing the name :'&' with the methods in the various Numeric subclasses, there's no meaning of mod. -- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Twitter: http://twitter.com/RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale
First
|
Prev
|
Pages: 1 2 Prev: Using roo to avoid need for Excel to be installed Next: meta-programming |