[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Shortening this expression ?

user@domain.invalid

11/24/2006 8:31:00 AM

Hello, I don't like the verbosity of these two lines

years = Array.new
2003.step(Time.now.year) { |a| years << a }

I would like to populate the year array on one statement but I don't
know how

Thanks in advance
9 Answers

KDr2

11/24/2006 8:54:00 AM

0

Zouplaz <user@domain.invalid> writes:

like this ? :

(2003..Time.now.year).to_a


> Hello, I don't like the verbosity of these two lines
>
> years = Array.new
> 2003.step(Time.now.year) { |a| years << a }
>
> I would like to populate the year array on one statement but I don't know how
>
> Thanks in advance

--
For some reasons,my EMail had been changed to "kdr2[#]163.com" now...

NO GNUS is Bad News.

------yours Killy Draw

Friedrich

11/24/2006 9:15:00 AM

0

Zouplaz <user@domain.invalid> writes:

> Hello, I don't like the verbosity of these two lines
>
> years = Array.new
> 2003.step(Time.now.year) { |a| years << a }
>
years = (2003...Time.now.year+1).entries

Is that "really" better?

Regards
Friedrich

--
Please remove just-for-news- to reply via e-mail.

KDr2

11/24/2006 9:30:00 AM

0

Friedrich Dominicus <just-for-news-frido@q-software-solutions.de>
writes:

> Zouplaz <user@domain.invalid> writes:
>
>> Hello, I don't like the verbosity of these two lines
>>
>> years = Array.new
>> 2003.step(Time.now.year) { |a| years << a }
>>
> years = (2003...Time.now.year+1).entries
maybe "a..b" is better than a...b+1 :P
>
> Is that "really" better?
>
> Regards
> Friedrich

--
For some reasons,my EMail had been changed to "kdr2[#]163.com" now...

NO GNUS is Bad News.

------yours Killy Draw

user@domain.invalid

11/24/2006 10:32:00 AM

0

le 24/11/2006 09:53, KDr2 nous a dit:
> Zouplaz <user@domain.invalid> writes:
>
> like this ? :
>
> (2003..Time.now.year).to_a
>
>

Wonderfull !

Friedrich

11/24/2006 10:56:00 AM

0

KDr2 <KDr2@163.com> writes:

> Friedrich Dominicus <just-for-news-frido@q-software-solutions.de>
> writes:
>
>> Zouplaz <user@domain.invalid> writes:
>>
>>> Hello, I don't like the verbosity of these two lines
>>>
>>> years = Array.new
>>> 2003.step(Time.now.year) { |a| years << a }
>>>
>> years = (2003...Time.now.year+1).entries
> maybe "a..b" is better than a...b+1 :P

depends
years = Array.new; 2003.step(Time.now.year) { |a| years << a }
irb(main):002:0> years
[2003, 2004, 2005, 2006]
irb(main):003:0> years = (2003...Time.now.year+1).entries
[2003, 2004, 2005, 2006]
irb(main):004:0>


I think +1 is better, YMMV

Regards
Friedrich

--
Please remove just-for-news- to reply via e-mail.

David Kastrup

11/24/2006 11:13:00 AM

0

Friedrich Dominicus <just-for-news-frido@q-software-solutions.de>
writes:

> KDr2 <KDr2@163.com> writes:
>
>> Friedrich Dominicus <just-for-news-frido@q-software-solutions.de>
>> writes:
>>
>>> Zouplaz <user@domain.invalid> writes:
>>>
>>>> Hello, I don't like the verbosity of these two lines
>>>>
>>>> years = Array.new
>>>> 2003.step(Time.now.year) { |a| years << a }
>>>>
>>> years = (2003...Time.now.year+1).entries
>> maybe "a..b" is better than a...b+1 :P
>
> depends
> years = Array.new; 2003.step(Time.now.year) { |a| years << a }
> irb(main):002:0> years
> [2003, 2004, 2005, 2006]
> irb(main):003:0> years = (2003...Time.now.year+1).entries
> [2003, 2004, 2005, 2006]
> irb(main):004:0>
>
>
> I think +1 is better, YMMV

You realize that "a..b" is the same as "a...b+1"?

--
David Kastrup, Kriemhildstr. 15, 44793 Bochum

Friedrich

11/24/2006 12:59:00 PM

0

David Kastrup <dak@gnu.org> writes:

>
> You realize that "a..b" is the same as "a...b+1"?
no. But nice to learn ;-)

Have a nice day
Friedrich



--
Please remove just-for-news- to reply via e-mail.

Robert Klemme

11/24/2006 11:33:00 PM

0

David Kastrup <dak@gnu.org> wrote:
> You realize that "a..b" is the same as "a...b+1"?

Note: For ints and in this case this is true. However it is not always
true:

irb(main):008:0> (1.0 .. (10.0+1)).include? 11.0
=> true
irb(main):009:0> (1.0 ... 11.0).include? 11.0
=> false

:-)

robert

William James

11/25/2006 11:47:00 AM

0


Robert Klemme wrote:
> David Kastrup <dak@gnu.org> wrote:
> > You realize that "a..b" is the same as "a...b+1"?
>
> Note: For ints and in this case this is true. However it is not always
> true:
>
> irb(main):008:0> (1.0 .. (10.0+1)).include? 11.0
> => true
> irb(main):009:0> (1.0 ... 11.0).include? 11.0
> => false
>
> :-)
>
> robert

irb(main):004:0> (1.0 .. 10.0).include? 10.5
=> false
irb(main):005:0> (1.0 ... (10.0+1)).include? 10.5
=> true