Forum: Ruby format an integer

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
40700f4fc57ab1ed04e235c7b7ae4d50?d=identicon&s=25 Me Me (melmoth)
on 2008-10-27 16:12
Hi,
this is a pretty silly question but I cannot figure it how to do.
I need to format an integer in order to have always two digits:

ex:
1 => 01
22 => 22

How can I do that in the shortest way possible?
Thanks in advance
A61ecce13ed142622f24a5ca3a123922?d=identicon&s=25 Matthew Moss (Guest)
on 2008-10-27 16:17
(Received via mailing list)
On Oct 27, 2008, at 10:11 AM, Me Me wrote:

> Hi,
> this is a pretty silly question but I cannot figure it how to do.
> I need to format an integer in order to have always two digits:
>
> ex:
> 1 => 01
> 22 => 22
>
> How can I do that in the shortest way possible?
> Thanks in advance


"%02d" % num
40700f4fc57ab1ed04e235c7b7ae4d50?d=identicon&s=25 Me Me (melmoth)
on 2008-10-27 16:33
> "%02d" % num

Thanks
104fcd49d9f843ec98e40883c2fc2a0d?d=identicon&s=25 Dan Webb (danw85)
on 2008-10-27 16:36
(Received via mailing list)
Q_txt = res_q[0][1]
  (0..10).each do |qt|
    question_text = q_txt.scan(/\w+/)[qt]
  end

when I access question_text after, obviously it's out of scope what am I
missing here?



Kind Regards,
Dan
F53b05cdbdf561cfe141f69b421244f3?d=identicon&s=25 David A. Black (Guest)
on 2008-10-27 17:36
(Received via mailing list)
Hi --

On Tue, 28 Oct 2008, Daniel Malcolm Webb [dbw] wrote:

> Q_txt = res_q[0][1]
>  (0..10).each do |qt|
>    question_text = q_txt.scan(/\w+/)[qt]
>  end
>
> when I access question_text after, obviously it's out of scope what am I
> missing here?

Blocks have a kind of one-way valve local scope. Variables that
already exist before the block will exist in the block. Variables that
are created in the block do not survive past the block.


David
Beb77c4602c3cac7a12149431366ed11?d=identicon&s=25 The Higgs bozo (higgsbozo)
on 2008-10-27 20:13
David A. Black wrote:
> On Tue, 28 Oct 2008, Daniel Malcolm Webb [dbw] wrote:
>> Q_txt = res_q[0][1]
>>  (0..10).each do |qt|
>>    question_text = q_txt.scan(/\w+/)[qt]
>>  end
>>
>> when I access question_text after, obviously it's out of scope what am I
>> missing here?
>
> Blocks have a kind of one-way valve local scope. Variables that
> already exist before the block will exist in the block. Variables that
> are created in the block do not survive past the block.

This is one reason I prefer {...} instead of do...end for blocks, since
for me the curly braces shout "new scope".  while...end, for...end,
until...end, if...end, unless...end do not introduce new scopes, yet
do...end does.

Though class...end, module...end, def...end also give new scopes, there
is little room for confusion because those are not control structures.
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2008-10-28 14:33
(Received via mailing list)
2008/10/27 David A. Black <dblack@rubypal.com>:
>> missing here?
Not what you asked for, but: "Q_txt" != "q_txt".  Also, you should do
the scan only once - this is more efficient:

texts = res_q[0][1].scan(/\w+/)
texts.each_with_index do |question_text, qt|
   ...
end

> Blocks have a kind of one-way valve local scope. Variables that
> already exist before the block will exist in the block. Variables that
> are created in the block do not survive past the block.

Which is basically the same in many modern programming languages,
isn't it?  Of course, there are some subtleties (e.g. whether
shadowing of more local definitions is allowed etc.).

Kind regards

robert
104fcd49d9f843ec98e40883c2fc2a0d?d=identicon&s=25 Dan Webb (danw85)
on 2008-10-28 14:54
(Received via mailing list)
Thanks Robert,

eventually managed another work around similar to yours. The Q/q was
indeed a typo that didn't make it into the final code. Help is
appreciated though :)

Kind Regards,
Dan
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2008-10-28 21:30
(Received via mailing list)
Robert Klemme wrote:
> 2008/10/27 David A. Black <dblack@rubypal.com>:
...
>> Blocks have a kind of one-way valve local scope. Variables that
>> already exist before the block will exist in the block. Variables that
>> are created in the block do not survive past the block.
>
> Which is basically the same in many modern programming languages,
> isn't it?  Of course, there are some subtleties (e.g. whether
> shadowing of more local definitions is allowed etc.).

That rules out javascript as modern. Grrr.
This topic is locked and can not be replied to.