[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

format an integer

Ema Fuma

10/27/2008 3:12:00 PM

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
--
Posted via http://www.ruby-....

8 Answers

Matthew Moss

10/27/2008 3:17:00 PM

0


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



Ema Fuma

10/27/2008 3:33:00 PM

0

> "%02d" % num

Thanks
--
Posted via http://www.ruby-....

Dan Webb

10/27/2008 3:36:00 PM

0

Q_txt =3D res_q[0][1]
(0..10).each do |qt|
question_text =3D 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





David A. Black

10/27/2008 4:36:00 PM

0

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

--
Rails training from David A. Black and Ruby Power and Light:
Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL
Advancing with Rails January 19-22 Fort Lauderdale, FL *
* Co-taught with Patrick Ewing!
See http://www.r... for details and updates!

The Higgs bozo

10/27/2008 7:13:00 PM

0

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.
--
Posted via http://www.ruby-....

Robert Klemme

10/28/2008 1:32:00 PM

0

2008/10/27 David A. Black <dblack@rubypal.com>:
> 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?

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

--
remember.guy do |as, often| as.you_can - without end

Dan Webb

10/28/2008 1:54:00 PM

0

Thanks Robert,=20

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

-----Original Message-----
From: Robert Klemme [mailto:shortcutter@googlemail.com]=20
Sent: 28 October 2008 13:32
To: ruby-talk ML
Subject: Re: Quick Scope Question

2008/10/27 David A. Black <dblack@rubypal.com>:
> Hi --
>
> On Tue, 28 Oct 2008, Daniel Malcolm Webb [dbw] wrote:
>
>> Q_txt =3D res_q[0][1]
>> (0..10).each do |qt|
>> question_text =3D q_txt.scan(/\w+/)[qt]
>> end
>>
>> when I access question_text after, obviously it's out of scope what
am I
>> missing here?

Not what you asked for, but: "Q_txt" !=3D "q_txt". Also, you should do
the scan only once - this is more efficient:

texts =3D 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

--=20
remember.guy do |as, often| as.you_can - without end


Joel VanderWerf

10/28/2008 8:29:00 PM

0

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.

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407