From: Ruby Newbee on
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 `<main>'



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

From: Ruby Newbee on
Oh sorry I have found that.
need to convert the arguments to an array.

"Hello, %s %s" %(["Matz!","again"])
=> "Hello, Matz! again"



On Mon, Dec 14, 2009 at 3:03 PM, Ruby Newbee <rubynewbee(a)gmail.com> wrote:
> 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 `<main>'
>
>
>
> So what's the correct syntax for this case?
> Thanks.
>

From: W. James on
Ruby Newbee wrote:

> Oh sorry I have found that.
> need to convert the arguments to an array.
>
> "Hello, %s %s" %(["Matz!","again"])
> => "Hello, Matz! again"
>
>
>
> On Mon, Dec 14, 2009 at 3:03 PM, Ruby Newbee <rubynewbee(a)gmail.com>
> wrote:
> > 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 `<main>'
> >
> >
> >
> > 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"

--

From: Hal Fulton on
[Note: parts of this message were removed to make it a legal post.]

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

From: Bertram Scharpf on
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.

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


--
Bertram Scharpf
Stuttgart, Deutschland/Germany
*
Discover String#notempty? at <http://raa.ruby-lang.org/project/step>.