[RCR] Numeric#of

L

Levin Alexander

T24gMi8xLzA2LCBQaGlsIER1YnkgPG5ld3Nob3VuZGF0QGF0cGhyaWVuZGx5LmF0bmV0PiB3cm90
ZToKCj4gSSB3YXMgbG9va2luZyBmb3IgYSBjbGVhbiAoUnVieSkgd2F5IG9mIGNyZWF0aW5nIGFu
IGFycmF5IG9mIGFzY2VuZGluZwo+IGludGVnZXIgdmFsdWVzLCB3aXRoIHRoZSBmaW5hbCB2YWx1
ZSB6ZXJvLiAgSUUgWyAxLCAyLCAzLCA0LCAwIF0KPiBpID0gMDsgYSA9IEFycmF5Lm5ldyggNCAp
IHsgaSArPSAxIH0ucHVzaCAwCgphID0gKDEuLjQpLnRvX2EgPDwgMAoKLUxldmluCg==
 
M

Mauricio Fernandez

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 Nakada <[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)
 
F

Florian Groß

Phil said:
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.
 
J

Joel VanderWerf

Adam said:
Bah, too long!
[""] * 42
Sorry, just had to jump in :)
.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.
 
A

ara.t.howard

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

this is __exactly__ why i made the RCR!

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

;-)

-a
 

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

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,662
Latest member
sxarexu

Latest Threads

Top