From: Simen Edvardsen Date: 2006-09-28T23:52:34+09:00 Subject: Re: New magical version of Symbol.to_proc On 9/28/06, dblack@wobblini.net wrote: > Hi -- > > On Thu, 28 Sep 2006, Robert Dober wrote: > > > > >> > >> > >> >>> (1..10).to_a.to_ss > >> > => ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"] > >> >>> (1..10).to_a.days > >> > => [86400, 172800, 259200, 345600, 432000, 518400, 604800, 691200, > >> > 777600, 864000] > >> >>> [2,'two', :two].classes > >> > => [Fixnum, String, Symbol] > >> >>> [2,'two', :two].classes.names > >> > => ["Fixnum", "String", "Symbol"] > >> >>> [2,'two', :two].classes.names.lengths > >> > => [6, 6, 6] > >> > > >> > So much happy syntax in one place! > >> > >> Actually the reason this is kind of cool is not the syntax but the > >> semantics. It's much more expressive than any number of &:->()... > >> things that have been proposed -- and looks better. (There may be > >> some connection there too.) > >> > >> I wonder, though, whether it would be too easy for it to trample on > >> other method names. I guess that's true of anything -- you'd just > >> have to make sure you didn't have two conceptions of some plural > >> thing. > > > > > > I agree, *cool* and *somehow dangerous*, I would very conservatively propose > > a proxy method, similiar in spirit to BDD's rspec#is > > > > %w{the meaning of 42}.map.length => [3, too_long_to_count, 2, 2] > > > > or maybe not overload #map without params for that purpose? > > I personally dislike "magic dot" stuff like that. True, it's not > really magic, since map can return some kind of enumerator that knows > about the array and is ready to do something to it. But it still has > the look and feel of a kind of secret system for communicating what's > wanted, rather than something that really makes sense when read left > to right. > > That's why I prefer Dr. Nic's plurals thing, though it definitely has > the disadvantage of entering into murky territority in terms of method > naming. > I like the idea of being able to treat messages like some kind of object and manipulating stuff with them somewhat like this, so just yesterday I made a mutated version of Symbol#to_proc, the "first class" message: class Message def initialize(message, *args) @message, @args = message, args end def call(x, *args) x.send @message, *(@args + args) end def to_proc lambda { |*args| call(*args) } end end if $0 == __FILE__ # Example puts [*0..100].select(&Message.new(:>, 50)).inspect puts [*0..100].inject(&Message.new(:+)) puts %w(Once upon a time).map(&Message.new(:upcase)).inspect puts Message.new(:index, /[aeiou]/, -3).call("hello") puts %w(1 2 3 4 5).map(&Message.new(:to_i)).map(&Message.new(:class)).inspect end $ ruby message.rb [51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100] 5050 ["ONCE", "UPON", "A", "TIME"] 4 [Fixnum, Fixnum, Fixnum, Fixnum, Fixnum] The syntax isn't as terse, but it does have the advantage of not interfering with existing methods (and you could just write a kernel method as a shortcut if you like). > > David > > -- > David A. Black | dblack@wobblini.net > Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3] > DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4] > [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com > [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org > > -- - Simen