[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

[RCR] Numeric#of

Ara.T.Howard

1/31/2006 11:06:00 PM

29 Answers

Yukihiro Matsumoto

1/31/2006 11:17:00 PM

0

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.


Jacob Fugal

1/31/2006 11:48:00 PM

0

On 1/31/06, Yukihiro Matsumoto <matz@ruby-lang.org> wrote:
> On 2/1/06, ara.t.howard@noaa.gov <ara.t.howard@noaa.gov> wrote:
> > 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.

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


matthew.moss.coder

1/31/2006 11:56:00 PM

0

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 }
=> ["", "", ""]


Jacob Fugal

2/1/2006 12:06:00 AM

0

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


Ara.T.Howard

2/1/2006 12:58:00 AM

0

James Gray

2/1/2006 1:28:00 AM

0

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


Ara.T.Howard

2/1/2006 2:01:00 AM

0

Phil Duby

2/1/2006 2:11:00 AM

0

"James Edward Gray II" <james@grayproductions.net> wrote in message
news:798D769A-AE90-4EE6-8D6D-F9A7A89C0CFE@grayproductions.net...
> 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
>
>
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.

--
Phil
remove all of the (at)'s to send email


Phil Duby

2/1/2006 2:15:00 AM

0

"James Edward Gray II" <james@grayproductions.net> wrote in message
news:798D769A-AE90-4EE6-8D6D-F9A7A89C0CFE@grayproductions.net...
> 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
>
>
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.

--
Phil
remove all of the (at)'s to send email


William James

2/1/2006 6:03:00 AM

0

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 { "" }

===> ["", "", ""]


--
future, n. That period of time in which our affairs prosper, our
friends are true, and our happiness is assured.
--- Ambrose Bierce