Numeric#of

Martin DeMello wrote:

also Array.new passes in the index to the block, so you can do

Array.new(4) {|i| i+1}

Here are some more alternatives including a solution with the famous
#inject. :slight_smile:

a=Array.new(5) {|i| (i+1)%5}
=> [1, 2, 3, 4, 0]

a=(1…4).to_a << 0
=> [1, 2, 3, 4, 0]

a=(1…5).inject([]) {|ar,i| ar << (i%5)}
=> [1, 2, 3, 4, 0]

a=(1…5).inject([]) {|ar,i| ar << i} << 0
=> [1, 2, 3, 4, 5, 0]

Have fun!

Kind regards

robert

Phil D. wrote:

I was looking for a clean (Ruby) way of creating an array of ascending
integer values, with the final value zero. IE [ 1, 2, 3, 4, 0 ]
i = 0; a = Array.new( 4 ) { i += 1 }.push 0

Two more ways of doing it:

Array.new(4) { |i| i + 1 } + [0]
(1 … 4).to_a + [0]

Too bad [*1…4, 0] doesn’t work.

On Thu, 2006-02-02 at 08:36 +0900, Florian GroÃ? wrote:

Too bad [*1…4, 0] doesn’t work.

Isn’t it? The best I got was:

[*1..4] << 0
# => 1, 2, 3, 4, 0

Adam S. wrote:

Bah, too long!
[""] * 42
Sorry, just had to jump in :slight_smile:
.adam sanderson

irb(main):018:0> a=[""]*42
=> ["", “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”,
“”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”,
“”, “”, “”, “”, “”, “”, “”]
irb(main):019:0> a[0]<<“I will not reuse string objects in class.”; puts
a
I will not reuse string objects in class.
I will not reuse string objects in class.
I will not reuse string objects in class.
I will not reuse string objects in class.
I will not reuse string objects in class.
I will not reuse string objects in class.
I will not reuse string objects in class.
.
.
.

:wink:

On Thu, Feb 02, 2006 at 01:03:30AM +0900, Jacob F. wrote:

On 1/31/06, Yukihiro M. [email protected] wrote:

If returns a enumerator object just like other enumerable methods do
if no block is given to them.

Sweet! I’ve wanted this sort of enumerator generation for a while. Is
this new across the board in 1.9, or have I just been stupid in
assuming it didn’t work when it has this whole time?

It’s been working for Enumerable (and similar) methods for a while (in
1.9):

Fri Jul 15 00:11:36 2005 Nobuyoshi N. [email protected]

* enum.c (enumeratorize): create new enumerator for current method if
  no block is given.

* enumerator.c: moved from ext/enumerator.

batsman@tux-chan:~/src/ruby/ruby.head$ grep -B 8 RETURN_ENUMERA -h *.c |
egrep ‘^\w+(’
rb_ary_index(int argc, VALUE *argv, VALUE ary)
rb_ary_rindex(int argc, VALUE *argv, VALUE ary)
rb_ary_each(VALUE ary)
rb_ary_each_index(VALUE ary)
rb_ary_reverse_each(VALUE ary)
rb_ary_collect(VALUE ary)
rb_ary_collect_bang(VALUE ary)
rb_ary_select(VALUE ary)
rb_ary_reject_bang(VALUE ary)
rb_ary_reject(VALUE ary)
dir_each(VALUE dir)
enum_find(int argc, VALUE *argv, VALUE obj)
enum_find_all(VALUE obj)
enum_reject(VALUE obj)
enum_collect(VALUE obj)
enum_partition(VALUE obj)
enum_sort_by(VALUE obj)
enum_min_by(VALUE obj)
enum_max_by(VALUE obj)
enum_each_with_index(VALUE obj)
rb_hash_select(VALUE hash)
rb_hash_each_value(VALUE hash)
rb_hash_each_key(VALUE hash)
rb_hash_each_pair(VALUE hash)
rb_hash_each(VALUE hash)
env_each_key(VALUE ehash)
env_each_value(VALUE ehash)
env_each(VALUE ehash)
env_each_pair(VALUE ehash)
env_select(VALUE ehash)
rb_io_each_line(int argc, VALUE *argv, VALUE io)
rb_io_each_byte(VALUE io)
rb_io_s_foreach(int argc, VALUE *argv, VALUE self)
argf_each_line(int argc, VALUE *argv, VALUE self)
argf_each_byte(VALUE self)
range_step(int argc, VALUE *argv, VALUE range)
range_each(VALUE range)
rb_str_each_byte(VALUE str)
rb_struct_each(VALUE s)
rb_struct_each_pair(VALUE s)

On Thu, 2 Feb 2006, Adam S. wrote:

Bah, too long!
[""] * 42
Sorry, just had to jump in :slight_smile:
.adam sanderson

this is exactly why i made the RCR!

harp:~ > ruby -e ’ a = [""] * 2; a.first << “42”; p a.last ’
“42”

:wink:

-a

On Feb 1, 2006, at 6:36 PM, Florian Groß wrote:

Too bad [*1…4, 0] doesn’t work.

Is this another YACC parsing problem or is there
some other conceptual reason why this would be prohibited?

Gary W.

On 2006-02-01, Florian Groß [email protected] wrote:

Too bad [*1…4, 0] doesn’t work.

[[*1…4], 0].flatten does.

Cheers,

Jeremy H.