Ted Leung on the air
Ted Leung on the air: Open Source, Java, Python, and ...
Ted Leung on the air: Open Source, Java, Python, and ...
Tue, 13 May 2003
Cedric on beautiful code
Cedric's post on beautiful code had a really interesting snippet of Ruby code in it.
[01:30] |
[computers/programming] |
# |
TB |
F |
G |
8 Comments |
File.new("foo.txt") { | f | # do something with f } # f gets automatically closedThis is an interesting use of blocks. I don't recall seeing this before, but it's very nice. Maybe one day I'll have some time to go play with Ruby. I know a few people who are into it, but Python seems to be the bomb at the moment. And I'm still trying to pry my cold dead fingers off of my parentheses, I mean, Lisp/Scheme code. One other comment in the whole beautiful code/source code as literature thread. My conception of the source code as literature idea was to look more at big programs that were worthy of study, not snippets. I guess if we wanted to take the analogy further, we should be looking at novel, short story, and poem equivalent sized programs.

You might like to have a look at blogtari http://www.jamesbritt.com/articles/blogatari.html which use to be a modified version of Cherry Blosxom, a port of blosxom done with Ruby, somehow that died, the link is no longer active.
This comment thingies are addictive, need to have them implemented on my site soon :)
Posted by wari at Tue May 13 02:55:02 2003
This comment thingies are addictive, need to have them implemented on my site soon :)
Posted by wari at Tue May 13 02:55:02 2003
I believe the Ruby code shown is syntactic sugar for the following (in Lisp since I don't know much Ruby):
<pre>
(file-new "foo.txt", #'(lambda (f) (do-something)))
>/pre>
That is the block after the function call is passed as the last argument to the function. Why is the given code more elegant than:
<pre>
File.new("foo.txt", { | f |
# do something with f
} )
</pre>
(No idea if that's valid Ruby but the idea is the same, passing the block as an argument rather than having it appear after the function).
Chris.
Posted by Chris Double at Tue May 13 05:20:05 2003
<pre>
(file-new "foo.txt", #'(lambda (f) (do-something)))
>/pre>
That is the block after the function call is passed as the last argument to the function. Why is the given code more elegant than:
<pre>
File.new("foo.txt", { | f |
# do something with f
} )
</pre>
(No idea if that's valid Ruby but the idea is the same, passing the block as an argument rather than having it appear after the function).
Chris.
Posted by Chris Double at Tue May 13 05:20:05 2003
Wari,
We can cut that release when ever you finish the Blogger code ;-)
Posted by Ted Leung at Tue May 13 10:03:52 2003
We can cut that release when ever you finish the Blogger code ;-)
Posted by Ted Leung at Tue May 13 10:03:52 2003
Chris,
It's not the sugar that appealed to me, it's the passing in of the block/closure. I just never thought about this as a way to design certain kinds of libraries, that's all.
Posted by Ted Leung at Tue May 13 10:05:12 2003
It's not the sugar that appealed to me, it's the passing in of the block/closure. I just never thought about this as a way to design certain kinds of libraries, that's all.
Posted by Ted Leung at Tue May 13 10:05:12 2003
It's a popular way of implementing macros in Dylan. At least from the code of browsed supplied from Functional Objects it seems fairly common. The function form is first defined:
define method file-new(name, fun)
let f = open(name);
block()
fun(f);
cleanup
close(f);
end;
end;
And then a macro implemented using it:
with-new-file(f, fn) /* do something*/ end;
Expands to:
block()
file-new(fn, method(f) /body of macro / end
end
This allows both the macro form and the function form to be used.
Posted by Chris Double at Tue May 13 14:12:38 2003
define method file-new(name, fun)
let f = open(name);
block()
fun(f);
cleanup
close(f);
end;
end;
And then a macro implemented using it:
with-new-file(f, fn) /* do something*/ end;
Expands to:
block()
file-new(fn, method(f) /body of macro / end
end
This allows both the macro form and the function form to be used.
Posted by Chris Double at Tue May 13 14:12:38 2003
Oops, my last comment has the code badly formatted. Sorry about that!
Posted by Chris Double at Tue May 13 14:13:28 2003
Posted by Chris Double at Tue May 13 14:13:28 2003
The good thing about blocks in Ruby is that they are used pervasively throughout the libraries. In Ruby, passing closures around is pretty much the default mode of operation for anything that works over a list, or runs in the context of some other operation.
For example, this code greps standard input, and runs the block against each matching line: I used it a while back to pull memory usage statistics out of a logfile:
Yes, you can do the same in Lisp with appropriate use of lambda. But that doesn't make Ruby any less cool. :)
Posted by Charles Miller at Tue May 13 18:20:47 2003
For example, this code greps standard input, and runs the block against each matching line: I used it a while back to pull memory usage statistics out of a logfile:
ARGF.grep /free: (\d+) total: (\d+)/ { |line| puts "#$2 #$1" }
Yes, you can do the same in Lisp with appropriate use of lambda. But that doesn't make Ruby any less cool. :)
Posted by Charles Miller at Tue May 13 18:20:47 2003
Now that's interesting... My hash-dollar-1 and hash-dollar-2 were munged by your comment sanitiser into just the hashes.
Posted by Charles Miller at Tue May 13 18:22:06 2003
Posted by Charles Miller at Tue May 13 18:22:06 2003
You can subscribe to an RSS feed of the comments for this blog:
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:
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

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