# How to write this correctly?

**URL:** <https://www.ruby-forum.com/t/how-to-write-this-correctly/180182>\
**Category:** Ruby\
**Created:** [December 14, 2009, 8:04am UTC](https://www.ruby-forum.com/t/how-to-write-this-correctly/180182 "2009-12-14T08:04:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [December 14, 2009, 8:04am UTC](https://www.ruby-forum.com/t/how-to-write-this-correctly/180182/1 "2009-12-14T08:04:42Z")

</div>

Hi,

In python this is right:

> > > “Hello, %s %s” %(“Matz!”,“again”)  
> > > ‘Hello, Matz! again’

But in ruby it will get wrong:

“Hello, %s %s” %(“Matz!”,“again”)  
SyntaxError: (irb):39: syntax error, unexpected ‘,’, expecting ‘)’  
“Hello, %s %s” %(“Matz!”,“again”)  
^  
from /usr/bin/irb:12:in `’

So what’s the correct syntax for this case?  
Thanks.

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [December 14, 2009, 8:06am UTC](https://www.ruby-forum.com/t/how-to-write-this-correctly/180182/2 "2009-12-14T08:06:28Z")

</div>

Oh sorry I have found that.  
need to convert the arguments to an array.

“Hello, %s %s” %([“Matz!”,“again”])  
=\> “Hello, Matz! again”

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [December 14, 2009, 8:15am UTC](https://www.ruby-forum.com/t/how-to-write-this-correctly/180182/3 "2009-12-14T08:15:49Z")

</div>

Ruby N. wrote:

> > Hi,  
> > SyntaxError: (irb):39: syntax error, unexpected ‘,’, expecting ‘)’  
> > “Hello, %s %s” %(“Matz!”,“again”)  
> > Â Â Â Â Â Â Â Â Â Â Â Â ^  
> > Â Â Â Â from /usr/bin/irb:12:in `’
> > 
> > So what’s the correct syntax for this case?  
> > Thanks.

“Hello, %s %s” % [“Matz!”, “again”]  
==\>“Hello, Matz! again”  
“Hello, %s %s” % %w(Matz! again)  
==\>“Hello, Matz! again”

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [December 14, 2009, 11:00am UTC](https://www.ruby-forum.com/t/how-to-write-this-correctly/180182/4 "2009-12-14T11:00:18Z")

</div>

Hi,

Am Montag, 14. Dez 2009, 16:05:54 +0900 schrieb Ruby N.:

> 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.

What you do is applying the mod(%) operator to a string:

str % array  
“%s %d %f” % [“hi”, 33, 0.618] #=\> “hi 33 0.618000”

Omit the parenthesis and write

“Hello, %s %s” % [“Matz!”,“again”]

or even

“Hello, %s %s” % %w(Matz! again)

Bertram

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [December 14, 2009, 9:30am UTC](https://www.ruby-forum.com/t/how-to-write-this-correctly/180182/5 "2009-12-14T09:30:45Z")

</div>

> “Hello, %s %s” % [“Matz!”, “again”]  
> ==\>“Hello, Matz! again”  
> “Hello, %s %s” % %w(Matz! again)  
> ==\>“Hello, Matz! again”

Or for those who prefer:

str = sprintf(“Hello, %s %s”, “Matz!”, “again”)

Or variations such as:

array = %w[Matz! again]  
sprintf(“Hello, %s %s”, \*array)

Or of course, printf will format and output as well:

printf(“Hello, %s %s”, “Matz!”, “again”)

Cheers,  
Hal

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [December 14, 2009, 3:28pm UTC](https://www.ruby-forum.com/t/how-to-write-this-correctly/180182/6 "2009-12-14T15:28:20Z")

</div>

On Mon, Dec 14, 2009 at 4:59 AM, Bertram S.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [Rick DeNatale - Developer - IBM | LinkedIn](http://www.linkedin.com/in/rickdenatale)
