How to write this correctly?

Discussion in 'Ruby' started by Ruby Newbee, Dec 14, 2009.

  1. Ruby Newbee

    Ruby Newbee Guest

    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.
    Ruby Newbee, Dec 14, 2009
    #1
    1. Advertising

  2. Ruby Newbee

    Ruby Newbee Guest

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

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



    On Mon, Dec 14, 2009 at 3:03 PM, Ruby Newbee <> 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")
    > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=

    =A0 =C2=A0 ^
    > =C2=A0 =C2=A0 =C2=A0 =C2=A0from /usr/bin/irb:12:in `<main>'
    >
    >
    >
    > So what's the correct syntax for this case?
    > Thanks.
    >
    Ruby Newbee, Dec 14, 2009
    #2
    1. Advertising

  3. Ruby Newbee

    W. James Guest

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

    --
    W. James, Dec 14, 2009
    #3
  4. Ruby Newbee

    Hal Fulton Guest

    [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
    Hal Fulton, Dec 14, 2009
    #4
  5. 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>.
    Bertram Scharpf, Dec 14, 2009
    #5
  6. On Mon, Dec 14, 2009 at 4:59 AM, Bertram Scharpf
    <> 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"])
    >> =3D> "Hello, Matz! again"

    >
    > Be aware that % is interpreted as an operator because of the
    > string in front of it. There is also a shortcut
    >
    > =A0%("Matz!","again")
    >
    > for
    >
    > =A0%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")
    =3D> "\"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 o=
    ne.

    Perhaps a subtle Ruby parsing/lexing bug.

    > What you do is applying the mod(%) operator to a string:
    >
    > =A0str % array
    > =A0"%s %d %f" % [ "hi", 33, 0.618] =A0 =A0 #=3D> "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.

    --=20
    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
    Rick DeNatale, Dec 14, 2009
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QWJlbCBDaGFu?=
    Replies:
    6
    Views:
    9,348
    Rahul
    May 3, 2006
  2. Peter Hansen
    Replies:
    1
    Views:
    384
    Joona I Palaste
    Aug 24, 2003
  3. JasBascom
    Replies:
    1
    Views:
    367
    John Harrison
    Feb 24, 2004
  4. Peter Hansen
    Replies:
    1
    Views:
    418
    Joona I Palaste
    Aug 24, 2003
  5. vinay
    Replies:
    12
    Views:
    2,011
    Athel Cornish-Bowden
    Aug 19, 2009
Loading...

Share This Page