[ANN] enumerable-extra 0.1.0

D

Daniel Berger

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
 
J

Joshua Collins

[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
 
D

Daniel Berger

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
 
M

Martin DeMello

= 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
 
D

Daniel Berger

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
 
R

Rick DeNatale

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.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
 
M

Michael Fellinger

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

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
473,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top