[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Alternative to the Symbol#to_proc hack

Ashley Moran

7/8/2006 7:42:00 PM

I've always been in two minds about the Symbol#to_proc hack, it's
clever but adds a bit of line noise. Then I figured you can turn it
around:

class String
def everything_in(e)
e.map { |u| u.send(self) }
end
end

> "upcase".everything_in %w( i am an array )
=> ["I", "AM", "AN", "ARRAY"]

It's a bit nasty because in 1.8 it can call private methods, but I
kinda like it. And it's not much use if you want to pass arguments
either.

Not sure if it's been done before either.

Ashley

8 Answers

dblack

7/8/2006 8:05:00 PM

0

Martin DeMello

7/8/2006 8:25:00 PM

0

On 7/9/06, dblack@wobblini.net <dblack@wobblini.net> wrote:
> Hi --
>
> On Sun, 9 Jul 2006, Ashley Moran wrote:
>
> > I've always been in two minds about the Symbol#to_proc hack, it's clever but
> > adds a bit of line noise. Then I figured you can turn it around:
> >
> > class String
> > def everything_in(e)
> > e.map { |u| u.send(self) }
> > end
> > end

you could do that neatly by trapping method_missing for String class
methods and matching against /^.*_all$/ :)

martin

Ashley Moran

7/8/2006 10:18:00 PM

0


On Jul 08, 2006, at 9:05 pm, dblack@wobblini.net wrote:

> Of course, I'm hopelessly non-edge -- I actually think that:
>
> %w{i am an array}.map {|item| item.upcase }
>
> is logical, reads nicely left to right, and contains no line noise :-)

True, but that only makes Java look old-fashioned. I wanted to make
Ruby look old-fashioned ;o)

Ever since I read about Higher-Order Messages at http://
nat.truemesh.com/archives/000537.html I've been determined to find a
way to write plain English as valid Ruby code. My goal is to find a
neat way to express business rules in Ruby, with as little framework
coding as possible. I think it may take a *little* more than
extending the String class though!

Ashley

dblack

7/8/2006 10:32:00 PM

0

Ashley Moran

7/9/2006 1:42:00 AM

0


On Jul 08, 2006, at 11:31 pm, dblack@wobblini.net wrote:

> Beware the dot, though. It can give you English, but at the expense
> of Ruby style. When I see:
>
> x = half.of.the.first.item.in(container)
>
> I want to know, and understand, what the message "the" means to the
> return value of a call to "of" -- and it isn't easy. Of course it's
> all legal Ruby, and can be documented... but it really sails beyond
> the horizon of what i would consider reasonable use of method-call
> syntax. In fact it's often a case of method-call syntax being used
> for method-*name* semantics.


David

That's a good point I hadn't considered. I don't know what to think
really. On the one had, I like code that is readable in the sense
that you can see what it does; on the other, I like to read code
where I can see *how it does it*. I think the former usually wins
though, or we'd all be programming in assembly.

Rails has got a lot of people using Ruby tricks that they don't fully
understand (eg collecting hashes and arrays in method definitions).
We're just starting to use it where I work, and first developer has
had to dive in and use Rails with only a token understanding of
Ruby. (Maybe I'm just weird - I prefer to understand how singleton
classes and method rewriting work before I go headfirst into crazy
stuff like Hello World!)

It's good that Ruby can be made so readable that you don't *need* to
understand it in depth to do useful stuff, but I think it will be the
undoing of many a newbie as they move to more ambitious projects.
I'm crossing my fingers that Rails doesn't become to web database
apps what Access is to desktop database apps! (What a terrible
thought to go to bed on...)

Ashley

dblack

7/9/2006 2:05:00 AM

0

M. Edward (Ed) Borasky

7/9/2006 3:53:00 AM

0

Ashley Moran wrote:
>
> I'm crossing my fingers that Rails doesn't become to web database apps
> what Access is to desktop database apps! (What a terrible thought to
> go to bed on...)
Access is great until your database gets a byte or 2 bigger than 2 GB. :)

--
M. Edward (Ed) Borasky

http://linuxcapacitypl...


Daniel DeLorme

7/9/2006 4:42:00 AM

0

Ashley Moran wrote:
> > "upcase".everything_in %w( i am an array )
> => ["I", "AM", "AN", "ARRAY"]

That's nice for map, but the to_proc hack can apply to ANY method. For example:
%w( i am an array ).sort_by(&:reverse)

Daniel