Enumerable-extra 0.1.0

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

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 :slight_smile:

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 :stuck_out_tongue:

On May 14, 11:30 am, Joshua C. [email protected] wrote:

at the same time – so, in a sense I am a newb :stuck_out_tongue:
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

On May 15, 1:47 am, Martin DeMello [email protected] wrote:

On Thu, May 14, 2009 at 9:38 PM, Daniel B. [email protected] 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?

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

On Thu, May 14, 2009 at 9:38 PM, Daniel B. [email protected]
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

On Thu, May 14, 2009 at 1:39 PM, Daniel B. [email protected]
wrote:

Thanks :slight_smile:

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) # =>
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.


Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

On Fri, 15 May 2009 01:08:12 +0900
“Daniel B.” [email protected] wrote:

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://pastr.it/15719

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs