Ted Leung on the air
Ted Leung on the air: Open Source, Java, Python, and ...
Tue, 15 Nov 2005
A wonderful hack?

This fall in our Bainbridge Island reading group, we are going through the Ruby on Rails and Ruby (Pickaxe) book. At our last meeting, one of the things that we discussed was Ruby closures, and I was trying to help people understand what was going on. Turns out there was one area where I didn't quite understand what was going on: the ability to pass an existing function as a block argument. I thought that you'd be able to do that, but apparently you can't.

A few days afterwards I finally got around to reading this post by Dave Thomas on the Symbol#to_proc hack, which I've excerpted:

The Ruby Extensions Project contains an absolutely wonderful hack. Say you want to convert an array of strings to uppercase. You could write

result = names.map {|name| name.upcase}

Fairly concise, right? Return a new array where each element is the corresponding element in the original, converted to uppercase. But if you include the Symbol extension from the Ruby Extensions Project, you could instead write

result = names.map(&:upcase)

Now that’s concise: apply the upcase method to each element of names.

Ouch. My idea of concise would have been:

result = names.map(upcase)

Or am I missing something here?

[23:33] | [computers/programming] | # | TB | F | G | 8 Comments | Other blogs commenting on this post

I still think most of the discussed alternatives are better than my pidgin JavaScript version:

map(compose(td, method("as_html")), this.prefs)

WRT the impossibility of extracting method objects from Ruby objects because the foo.bar syntax already has a meaning: there are several languages (Lisp, FORTH, PostScript) in which you can use some contextual marker (', ', and / respectively) to get the name of something rather than invoking it.  Perhaps in Unicode we should use a pentagram for this function.  One could certainly alter Ruby's syntax in this way, at least in theory.
Posted by Kragen Sitaker at Wed Nov 30 19:15:14 2005

You can subscribe to an RSS feed of the comments for this blog: RSS Feed for comments

Add a comment here:

You can use some HTML tags in the comment text:
To insert a URI, just type it -- no need to write an anchor tag.
Allowable html tags are: <a href>, <em>, <i>, <b>, <blockquote>, <br/>, <p>, <code>, <pre>, <cite>, <sub> and <sup>.

You can also use some Wiki style:
URI => [uri title]
<em> => _emphasized text_
<b> => *bold text*
Ordered list => consecutive lines starting spaces and an asterisk





Remember my info?

twl JPG


Ted Leung FOAF Explorer

I work at the Open Source Applications Foundation (OSAF).
The opinions expressed here are entirely my own, not those of my employer.

Creative Commons License
This work is licensed under a Creative Commons License.

Now available!
Professional XML Development with Apache Tools : Xerces, Xalan, FOP, Cocoon, Axis, Xindice
Technorati Profile
PGP Key Fingerprint
My del.icio.us Bookmarks
My Flickr Photos

RSS 2.0 xml GIF
Comments (RSS 2.0) xml GIF
Atom 0.3 feed
Feedburner'ed RSS feed

< November 2005 >
   1 2 3 4 5
6 7 8 9101112


Macintosh Tips and Tricks

Blogs nearby
geourl PNG

/ (1567)
  books/ (33)
  computers/ (62)
    hardware/ (15)
    internet/ (58)
      mail/ (11)
      microcontent/ (58)
      weblogs/ (174)
        pyblosxom/ (36)
      www/ (25)
    open_source/ (145)
      asf/ (53)
      osaf/ (32)
        chandler/ (35)
        cosmo/ (1)
    operating_systems/ (16)
      linux/ (9)
        debian/ (15)
        ubuntu/ (2)
      macosx/ (101)
        tips/ (25)
      windows_xp/ (4)
    programming/ (156)
      clr/ (1)
      dotnet/ (13)
      java/ (71)
        eclipse/ (22)
      lisp/ (34)
      python/ (86)
      smalltalk/ (4)
      xml/ (18)
    research/ (1)
    security/ (4)
    wireless/ (1)
  culture/ (10)
    film/ (8)
    music/ (6)
  education/ (13)
  family/ (17)
  gadgets/ (24)
  misc/ (47)
  people/ (18)
  photography/ (25)
    pictures/ (12)
  places/ (3)
    us/ (0)
      wa/ (2)
        bainbridge_island/ (17)
        seattle/ (13)
  skating/ (6)
  society/ (20)

[Valid RSS]

del.icio.us linkblog



Listed on BlogShares

Locations of visitors to this page
Where are visitors to this page?

pyblosxom GIF