Shortening this expression ?

Z

Zouplaz

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
 
K

KDr2

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
 
F

Friedrich Dominicus

Zouplaz said:
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
 
K

KDr2

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
 
F

Friedrich Dominicus

KDr2 said:
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
 
D

David Kastrup

KDr2 said:
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"?
 
R

Robert Klemme

David Kastrup said:
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
 
W

William James

Robert said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top