Format strings?

Having an int object, let's say the value is 2, I'd like to print
it out as "0002". I miss C's format strings. Any hints ? Thanks in
advance.

···

--
If it's there, and you can see it, it's real.
If it's not there, and you can see it, it's virtual.
If it's there, and you can't see it, it's transparent.
If it's not there, and you can't see it, you erased it.

Kernel#sprintf ?

http://www.ruby-doc.org/core/classes/Kernel.html#M002983

def format3(int)
  fmtStr = sprintf("%04d", int)
  print "#{fmtStr}\n"
end

format3(2)

-Madan.

···

On 5/23/06, Vlad GALU <vladgalu@gmail.com> wrote:

   Having an int object, let's say the value is 2, I'd like to print
it out as "0002". I miss C's format strings. Any hints ? Thanks in
advance.

   Having an int object, let's say the value is 2, I'd like to print
it out as "0002". I miss C's format strings. Any hints ? Thanks in
advance.

  Ah I found Kernel#printf, sorry for the noise :slight_smile:

···

On 5/23/06, Vlad GALU <vladgalu@gmail.com> wrote:

--
If it's there, and you can see it, it's real.
If it's not there, and you can see it, it's virtual.
If it's there, and you can't see it, it's transparent.
If it's not there, and you can't see it, you erased it.

--
If it's there, and you can see it, it's real.
If it's not there, and you can see it, it's virtual.
If it's there, and you can't see it, it's transparent.
If it's not there, and you can't see it, you erased it.

  Having an int object, let's say the value is 2, I'd like to print
it out as "0002". I miss C's format strings. Any hints ? Thanks in
advance.

Miss them no longer:

>> value = 2
=> 2
>> sprintf "%04d", value
=> "0002"
>> "%04d" % value
=> "0002"

:wink:

James Edward Gray II

···

On May 23, 2006, at 9:16 AM, Vlad GALU wrote:

printf "%04d", 2

Ruby has format strings!
   -tim

···

On 5/23/06, Vlad GALU <vladgalu@gmail.com> wrote:

   Having an int object, let's say the value is 2, I'd like to print
it out as "0002". I miss C's format strings.

Another option:

irb(main):001:0> '%04d' % 2
=> "0002"

···

On 5/23/06, Vlad GALU <vladgalu@gmail.com> wrote:

   Having an int object, let's say the value is 2, I'd like to print
it out as "0002". I miss C's format strings. Any hints ? Thanks in
advance.

> Having an int object, let's say the value is 2, I'd like to print
> it out as "0002". I miss C's format strings.

printf "%04d", 2

Ruby has format strings!
   -tim

   Thank you all! I noticed soon after I posted the question *blush* :slight_smile:

···

On 5/23/06, Tim Becker <a2800276@gmail.com> wrote:

On 5/23/06, Vlad GALU <vladgalu@gmail.com> wrote:

--
If it's there, and you can see it, it's real.
If it's not there, and you can see it, it's virtual.
If it's there, and you can't see it, it's transparent.
If it's not there, and you can't see it, you erased it.