Forum: Ruby Numeric#of

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 unknown (Guest)
on 2006-02-01 00:06
(Received via mailing list)
i threw this out before.  thought i'd try again:

   harp:~ > irb
   irb(main):001:0> class Numeric; def of(&b) Array::new(self).map(&b)
end; end
   => nil

   irb(main):002:0> 42.of{ String::new }
   => ["", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", ""]


regards.

-a
0ec4920185b657a03edf01fff96b4e9b?d=identicon&s=25 Yukihiro Matsumoto (Guest)
on 2006-02-01 00:18
(Received via mailing list)
Hi,

On 2/1/06, ara.t.howard@noaa.gov <ara.t.howard@noaa.gov> wrote:

> i threw this out before.  thought i'd try again:
>
>    harp:~ > irb
>    irb(main):001:0> class Numeric; def of(&b) Array::new(self).map(&b) end; end
>    => nil
>
>    irb(main):002:0> 42.of{ String::new }
>    => ["", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]

Using my not-yet-committed 1.9, you can type

  42.times.collect{String::new}

to get the same result.

matz.
Cff9eed5d8099e4c2d34eae663aae87e?d=identicon&s=25 Jacob Fugal (Guest)
on 2006-02-01 00:48
(Received via mailing list)
On 1/31/06, Yukihiro Matsumoto <matz@ruby-lang.org> wrote:
>   42.times.collect{String::new}
>
> to get the same result.

Cool!

So does times still work with a block? I'm assuming that it switches
behavior, being a generator itself if a block is present, and creating
a generator object (which includes Enumerable) when no block is
provided.

Eg. (simplified):

  class Numeric
    def times( &blk )
      generator = # some code to create the generator
                  # that runs from 0 to self-1
      generator.each &blk if block_given?
      generator
    end
  end

If that's the case, this is awesome!

Jacob Fugal
81d609425e306219d54d793a0ad98bce?d=identicon&s=25 Matthew Moss (Guest)
on 2006-02-01 00:57
(Received via mailing list)
All I did was

class Integer
  def times
    if block_given?
      (0...self).each { |i| yield i }
    else
      (0...self)
    end
  end
end

3.times.collect { String.new }
=> ["", "", ""]
Cff9eed5d8099e4c2d34eae663aae87e?d=identicon&s=25 Jacob Fugal (Guest)
on 2006-02-01 01:06
(Received via mailing list)
On 1/31/06, Matthew Moss <matthew.moss.coder@gmail.com> wrote:
> class Integer
>   def times
>     if block_given?
>       (0...self).each { |i| yield i }
>     else
>       (0...self)
>     end
>   end
> end

Yeah, that's pretty much the same thing as my pseudo-code if you let
generator = (0...self) which, given the special implementation of
Fixnum ranges, is probably the best solution. Note that it Matz
probably did do it in Fixnum, not Numeric (How would you do something
Math::PI.times? :) or Integer.

Jacob Fugal
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 unknown (Guest)
on 2006-02-01 02:01
(Received via mailing list)
On Wed, 1 Feb 2006, Yukihiro Matsumoto wrote:

>>    irb(main):002:0> 42.of{ String::new }
>>    => ["", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]
>
> Using my not-yet-committed 1.9, you can type
>
>  42.times.collect{String::new}
>
> to get the same result.

great!  i'm still partial to the shorter version since

   42.times.collect{String::new}

isn't that much shorter/prettier than my current idiom of

   Array::new(42).map{String::new}

but it's the capability that's needed so this is good news.

kind regards.

-a
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2006-02-01 02:28
(Received via mailing list)
On Jan 31, 2006, at 6:58 PM, ara.t.howard@noaa.gov wrote:

>   Array::new(42).map{String::new}

That can be shortened to:

 >> Array.new(42) { String.new }
=> ["", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", ""]

James Edward Gray II
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 unknown (Guest)
on 2006-02-01 03:01
(Received via mailing list)
On Wed, 1 Feb 2006, James Edward Gray II wrote:

> On Jan 31, 2006, at 6:58 PM, ara.t.howard@noaa.gov wrote:
>
>>   Array::new(42).map{String::new}
>
> That can be shortened to:
>
>>> Array.new(42) { String.new }
> => ["", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
> "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
> "", "", "", "", ""]

indeed.  my bad.

none still as pretty as '42.of' though!  ;-)

-a
0ec4920185b657a03edf01fff96b4e9b?d=identicon&s=25 Yukihiro Matsumoto (Guest)
on 2006-02-01 07:27
(Received via mailing list)
Hi,

On 2/1/06, Jacob Fugal <lukfugl@gmail.com> wrote:
> On 1/31/06, Yukihiro Matsumoto <matz@ruby-lang.org> wrote:

> > Using my not-yet-committed 1.9, you can type
> >
> >   42.times.collect{String::new}
> >
> > to get the same result.
>
> So does times still work with a block?

Yes.

> I'm assuming that it switches
> behavior, being a generator itself if a block is present, and creating
> a generator object (which includes Enumerable) when no block is
> provided.

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

matz.
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2006-02-01 07:33
(Received via mailing list)
On Feb 1, 2006, at 1:26 AM, Yukihiro Matsumoto wrote:

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

How deep does this rabbit hole go?

Does map return an enumerable without a block? And if so, can we type
obj.map.map.map... til the end of time?
0ec4920185b657a03edf01fff96b4e9b?d=identicon&s=25 Yukihiro Matsumoto (Guest)
on 2006-02-01 07:51
(Received via mailing list)
Hi,

On 2/1/06, Logan Capaldo <logancapaldo@gmail.com> wrote:

> How deep does this rabbit hole go?

As deep as you want, and the machine allows

> Does map return an enumerable without a block? And if so, can we type
> obj.map.map.map... til the end of time?

Yes.

matz.
Cff9eed5d8099e4c2d34eae663aae87e?d=identicon&s=25 Jacob Fugal (Guest)
on 2006-02-01 17:05
(Received via mailing list)
On 1/31/06, Yukihiro Matsumoto <matz@ruby-lang.org> 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?

Jacob Fugal
7f649f7754960581ea9d7f7ee518d782?d=identicon&s=25 Phil Duby (Guest)
on 2006-02-02 00:02
(Received via mailing list)
"James Edward Gray II" <james@grayproductions.net> wrote in message
news:798D769A-AE90-4EE6-8D6D-F9A7A89C0CFE@grayproductions.net...
>
> James Edward Gray II
>
>
Thank for these!!

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
does what I want.
7f649f7754960581ea9d7f7ee518d782?d=identicon&s=25 Phil Duby (Guest)
on 2006-02-02 00:02
(Received via mailing list)
"James Edward Gray II" <james@grayproductions.net> wrote in message
news:798D769A-AE90-4EE6-8D6D-F9A7A89C0CFE@grayproductions.net...
>
> James Edward Gray II
>
>
Thank for these!!

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
does what I want.
2ee1a7960cc761a6e92efb5000c0f2c9?d=identicon&s=25 William James (Guest)
on 2006-02-02 00:11
(Received via mailing list)
ara.t.howard@noaa.gov wrote:
> i threw this out before.  thought i'd try again:
>
>    harp:~ > irb
>    irb(main):001:0> class Numeric; def of(&b) Array::new(self).map(&b) end; end
>    => nil
>
>    irb(main):002:0> 42.of{ String::new }
>    => ["", "", "", "", "", "", "", "", "", "",   . . .

class Fixnum
  def of
    (0...self).map{ yield }
  end
end

p  3.of { "" }

===>  ["", "", ""]
45196398e9685000d195ec626d477f0e?d=identicon&s=25 Trans (Guest)
on 2006-02-02 00:14
(Received via mailing list)
The coming dawn of Role-Oriented Progamming is upon us (such is the way
of the Functor).
2cf6d8e639314abd751f83a72e9a2ac5?d=identicon&s=25 Martin DeMello (Guest)
on 2006-02-02 00:29
(Received via mailing list)
Phil Duby <newshoundat@atphriendly.atnet> 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
> does what I want.

(1..4).to_a.push(0)

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

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

martin
5befe95e6648daec3dd5728cd36602d0?d=identicon&s=25 Robert Klemme (Guest)
on 2006-02-02 00:29
(Received via mailing list)
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. :-)

>> 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
5befe95e6648daec3dd5728cd36602d0?d=identicon&s=25 Robert Klemme (Guest)
on 2006-02-02 00:32
(Received via mailing list)
Robert Klemme wrote:
>>
> => [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]

Pardon me, the last one should've read:

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

    robert
65bd9e4c5aebde25ebf16d599339d570?d=identicon&s=25 Adam Sanderson (Guest)
on 2006-02-02 00:50
(Received via mailing list)
Bah, too long!
  [""] * 42
Sorry, just had to jump in :)
  .adam sanderson
0d298cda3121e5cacaa2465437769025?d=identicon&s=25 Levin Alexander (Guest)
on 2006-02-02 01:33
(Received via mailing list)
On 2/1/06, Phil Duby <newshoundat@atphriendly.atnet> 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

a = (1..4).to_a << 0

-Levin
Dd76a12d66f843de5c5f8782668e7127?d=identicon&s=25 Mauricio Fernandez (Guest)
on 2006-02-02 01:36
(Received via mailing list)
On Thu, Feb 02, 2006 at 01:03:30AM +0900, Jacob Fugal wrote:
> On 1/31/06, Yukihiro Matsumoto <matz@ruby-lang.org> 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 Nakada  <nobu@ruby-lang.org>

	* 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)
E0ed615bd6632dd23165e045e3c1df09?d=identicon&s=25 Florian GroÃ? (Guest)
on 2006-02-02 01:39
(Received via mailing list)
Phil Duby 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.
A9b6a93b860020caf9d2d1d58c32478f?d=identicon&s=25 Ross Bamford (Guest)
on 2006-02-02 02:40
(Received via mailing list)
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
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2006-02-02 07:15
(Received via mailing list)
Adam Sanderson wrote:
> 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.
.
.
.



;)
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 unknown (Guest)
on 2006-02-02 16:15
(Received via mailing list)
On Thu, 2 Feb 2006, Adam Sanderson wrote:

> 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
7fd7ef91196768cae4e7ca215c8a0a8f?d=identicon&s=25 Jeremy Henty (Guest)
on 2006-02-02 22:24
(Received via mailing list)
On 2006-02-01, Florian Groß <florgro@gmail.com> wrote:

> Too bad [*1..4, 0] doesn't work.

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

Cheers,

Jeremy Henty
E7559e558ececa67c40f452483b9ac8c?d=identicon&s=25 unknown (Guest)
on 2006-02-03 00:31
(Received via mailing list)
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 Wright
This topic is locked and can not be replied to.