Printing to a format string

Discussion in 'Ruby' started by jesse.r.brown@gmail.com, May 20, 2006.

  1. Guest

    I have run across something that I can quite figure out at the moment.
    Here is the section of code I am dealing with:

    # construct a string that can be
    # output in the structure definition
    # [depth] is the current tab depth
    # [tab] is the tab width
    def struct_str(depth,tab)
    indent = " " * tab * depth
    str = indent + "\"#@name\", \"#@fmt\", #@args,\n"
    str += indent + "%15s %10s - #@err_msg \n" % [@name, @fmt]

    I get an error when trying to assign @fmt to the %10s.
    I _think_ the problem is that I have, in the @fmt variable, a c-style
    printf format string. The error I get is:

    ../commands.rb:39:in `%': too few arguments (ArgumentError)

    Am I doing something incorrectly? Or is there an easy way around this?

    Note:
    The @fmt come from a user-provided file, so careless translations will
    lead to undefined effects. I want to try and fix this localy if
    possible.

    Thanks in advance for any help.
     
    , May 20, 2006
    #1
    1. Advertising

  2. F. Senault Guest

    Le 20 mai 2006 à 21:17, a écrit :

    > str += indent + "%15s %10s - #@err_msg \n" % [@name, @fmt]
    >
    > I get an error when trying to assign @fmt to the %10s.
    > I _think_ the problem is that I have, in the @fmt variable, a c-style
    > printf format string. The error I get is:


    Are you sure it isn't your @err_msg that contains "%" ? I'd write :

    str += indent + "%15s %10s - %s \n" % [@name, @fmt, @err_msg]

    Fred
    --
    Feel my blood enraged It's just the fear of losing you
    Don't you know my name Well, you been so long
    (David Bowie, Cat People - Putting Out Fire)
     
    F. Senault, May 20, 2006
    #2
    1. Advertising

  3. Guest

    Yes, thank you.
    How foolish I feel now after having seen the solution...
     
    , May 21, 2006
    #3
    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. Brian Candy
    Replies:
    2
    Views:
    1,173
    Janaka
    Feb 18, 2004
  2. ankur
    Replies:
    1
    Views:
    12,813
    Jan =?UTF-8?B?VGhvbcOk?=
    Aug 27, 2007
  3. Chris Angelico
    Replies:
    3
    Views:
    157
    Mark Lawrence
    Mar 1, 2013
  4. Peter Otten
    Replies:
    0
    Views:
    134
    Peter Otten
    Feb 28, 2013
  5. Rick Johnson
    Replies:
    0
    Views:
    142
    Rick Johnson
    Feb 28, 2013
Loading...

Share This Page