On Mon, Jul 18, 2011 at 8:28 AM, raju sathliya <raju.sathliya / gmail.com>wrote:

> Josh Cheek wrote in post #1011386:
> > On Mon, Jul 18, 2011 at 7:52 AM, raju sathliya
> > <raju.sathliya / gmail.com>wrote:
> >
> >>
> >> Thanks,
> >> raju
> >>
> >> --
> >> Posted via http://www.ruby-forum.com/.
> >>
> >>
> > You want printf http://ruby-doc.org/core/classes/IO.html#M000894 There
> > is a
> > breakdown of everything it can do on sprintf
> > http://ruby-doc.org/core/classes/Kernel.html#M001432
> >
> > In this particular case, it would be `printf('%03d', val)`
>
> Thanks Josh for reply !
> But for variable it gives me below error
>
> test2.rb:46: syntax error, unexpected ',', expecting ')'
> sprintf ('%03d',"#{num}")
>                ^
> test2.rb:46: syntax error, unexpected ')', expecting $end
>
> Do you have any solution for this please
>
> --
> Posted via http://www.ruby-forum.com/.
>
>
1. You shouldn't have spaces between your method calls and the parentheses
(note that you can omit the parens)
2. printf sends result to stdout, sprintf returns the string. If you just
want a string, then sprintf is right (or the % operator as Steve showed
above), but in your example earlier, you were trying to print it.
3. When you do "#{num}" you end up converting num into a string. There is no
need for this as the "d" tells it you will be passing a number.