[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

[ANN] enumerable-extra 0.1.0

Daniel Berger

5/14/2009 4:08:00 PM

Hi,

Convinced that Symbol#to_proc is mostly a solution in search of a problem,
I've released enumerable-extra 0.1.0.

= What is it?

The enumerable-extra library overrides Enumerable#map, Array#map and
Array#map to accept a method, with optional arguments, that are performed on
every element of the list.

For example, instead of this:

array.map{ |e| e.capitalize }

You can do this:

array.map(:capitalize) # Capitalize every element

You can also mix and match:

array.map(:capitalize){ |e| e + 'x' } # Add 'x' after capitalizing every
element

= Why is it better than Symbol#to_proc ?

First, it doesn't require additional notation, i.e. no "&", no "its", no
"self" - just a method name. In my opinion, that makes the syntax cleaner.
Second, Symbol#to_proc is slow.

= How do I get it?

gem install enumerable-extra

= Caveat

This is for Ruby 1.8.x only.

Enjoy!

Dan


6 Answers

Joshua Collins

5/14/2009 5:31:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

Would this be usable with a RoR app?

I am not certain if RoR core needs Symbol#to_proc ... but, it would be nice
to use enumerable-extra in my Ruby coding in RoR. I am just iffy about
trying it cause I am not certain if I would break my RoR app because I
installed the gem and the gem overirides Enumberable#map and so forth.

Thanks :)

Sorry if it seems like a newb question, but I am learning both Ruby and RoR
at the same time -- so, in a sense I am a newb :-P

On Thu, May 14, 2009 at 12:08 PM, Daniel Berger <djberg96@gmail.com> wrote:

> Hi,
>
> Convinced that Symbol#to_proc is mostly a solution in search of a problem,
> I've released enumerable-extra 0.1.0.
>
> = What is it?
>
> The enumerable-extra library overrides Enumerable#map, Array#map and
> Array#map to accept a method, with optional arguments, that are performed
> on
> every element of the list.
>
> For example, instead of this:
>
> array.map{ |e| e.capitalize }
>
> You can do this:
>
> array.map(:capitalize) # Capitalize every element
>
> You can also mix and match:
>
> array.map(:capitalize){ |e| e + 'x' } # Add 'x' after capitalizing every
> element
>
> = Why is it better than Symbol#to_proc ?
>
> First, it doesn't require additional notation, i.e. no "&", no "its", no
> "self" - just a method name. In my opinion, that makes the syntax cleaner.
> Second, Symbol#to_proc is slow.
>
> = How do I get it?
>
> gem install enumerable-extra
>
> = Caveat
>
> This is for Ruby 1.8.x only.
>
> Enjoy!
>
> Dan
>
>
>

Daniel Berger

5/14/2009 5:40:00 PM

0



On May 14, 11:30=A0am, Joshua Collins <kidg...@gmail.com> wrote:
> Would this be usable with a RoR app?
>
> I am not certain if RoR core needs Symbol#to_proc ... but, it would be ni=
ce
> to use enumerable-extra in my Ruby coding in RoR. I am just iffy about
> trying it cause I am not certain if I would break my RoR app because I
> installed the gem and the gem overirides Enumberable#map and so forth.
>
> Thanks :)
>
> Sorry if it seems like a newb question, but I am learning both Ruby and R=
oR
> at the same time -- so, in a sense I am a newb :-P

It might, I haven't tried. It might depend on the order of your
require statements, too, because I haven't looked at their
implementation.

The best way to find out is to run your tests. I'm curious about the
results myself.

Note that, with no arguments it works the same as the standard MRI
version, so you can still do this:

array.map{ |e| e.capitalize }

Regards,

Dan

Martin DeMello

5/15/2009 7:47:00 AM

0

On Thu, May 14, 2009 at 9:38 PM, Daniel Berger <djberg96@gmail.com> wrote:
>
> = Why is it better than Symbol#to_proc ?
>
> First, it doesn't require additional notation, i.e. no "&", no "its", no
> "self" - just a method name. In my opinion, that makes the syntax cleaner.
> Second, Symbol#to_proc is slow.

This looks very nice! What would it take to port to ruby 1.9?

martin

Daniel Berger

5/15/2009 1:55:00 PM

0



On May 15, 1:47=A0am, Martin DeMello <martindeme...@gmail.com> wrote:
> On Thu, May 14, 2009 at 9:38 PM, Daniel Berger <djber...@gmail.com> wrote=
:
>
> > =3D Why is it better than Symbol#to_proc ?
>
> > First, it doesn't require additional notation, i.e. no "&", no "its", n=
o
> > "self" - just a method name. In my opinion, that makes the syntax clean=
er.
> > Second, Symbol#to_proc is slow.
>
> This looks very nice! What would it take to port to ruby 1.9?

I'm not sure. I declared that it was for 1.8.x only because I hadn't
tested it with 1.9.x and I wasn't sure if it would break its builtin
Symbol#to_proc support, or what would happen.

I'll do some testing later this week with 1.9.x and see what happens.

Regards,

Dan

Rick DeNatale

5/15/2009 2:37:00 PM

0

On Thu, May 14, 2009 at 1:39 PM, Daniel Berger <djberg96@gmail.com> wrote:
>
>
> On May 14, 11:30=A0am, Joshua Collins <kidg...@gmail.com> wrote:
>> Would this be usable with a RoR app?
>>
>> I am not certain if RoR core needs Symbol#to_proc ... but, it would be n=
ice
>> to use enumerable-extra in my Ruby coding in RoR. I am just iffy about
>> trying it cause I am not certain if I would break my RoR app because I
>> installed the gem and the gem overirides Enumberable#map and so forth.
>>
>> Thanks :)
>>
>> Sorry if it seems like a newb question, but I am learning both Ruby and =
RoR
>> at the same time -- so, in a sense I am a newb :-P
>
> It might, I haven't tried. It might depend on the order of your
> require statements, too, because I haven't looked at their
> implementation.
>
> The best way to find out is to run your tests. I'm curious about the
> results myself.
>
> Note that, with no arguments it works the same as the standard MRI
> version, so you can still do this:
>
> array.map{ |e| e.capitalize }

There SHOULDN'T be a conflict since Rails/ActiveSupport doesn't change
the map method it only adds a to_proc method to Symbol.

require 'rubygems'
require 'activesupport'
require 'enumerable/extra'

p %w{a b c}.map(:upcase)
p %w{a b c}.map(&:upcase) # =3D>
p %w{a b c}.map(:upcase, &:downcase)

["A", "B", "C"]
["A", "B", "C"]
["a", "b", "c"]

Now whether or not combining these is a good idea, I don't know.

--=20
Rick DeNatale

Blog: http://talklikeaduck.denh...
Twitter: http://twitter.com/Ri...
WWR: http://www.workingwithrails.com/person/9021-ric...
LinkedIn: http://www.linkedin.com/in/ri...

Michael Fellinger

5/15/2009 4:00:00 PM

0

On Fri, 15 May 2009 01:08:12 +0900
"Daniel Berger" <djberg96@gmail.com> wrote:

> Hi,
>
> Convinced that Symbol#to_proc is mostly a solution in search of a
> problem, I've released enumerable-extra 0.1.0.
>
> = What is it?
>
> The enumerable-extra library overrides Enumerable#map, Array#map and
> Array#map to accept a method, with optional arguments, that are
> performed on every element of the list.
>
> For example, instead of this:
>
> array.map{ |e| e.capitalize }
>
> You can do this:
>
> array.map(:capitalize) # Capitalize every element
>
> You can also mix and match:
>
> array.map(:capitalize){ |e| e + 'x' } # Add 'x' after capitalizing
> every element
>
> = Why is it better than Symbol#to_proc ?
>
> First, it doesn't require additional notation, i.e. no "&", no "its",
> no "self" - just a method name. In my opinion, that makes the syntax
> cleaner. Second, Symbol#to_proc is slow.

Sorry, but Symbol#to_proc is actually the faster way since it was added into
the core, so your statement is only true for < 1.8.6.

Benchmark at: http://past...

--
^ manveru