Whither SmallScript? (was Re: Integer micro-benchmarks)
Johann Hibschman
johann at physics.berkeley.edu
Fri Apr 27 14:32:15 EDT 2001
Steven D Majewski writes:
>> class Foo < Bar
>> def myMethod
>> File.new ("test.dat", "w") { |f|
>> f.puts "Hello world!"
>> }
>> end
>> end
> What do the vertical bars around the f ("|f|") above indicate ?
That indicates that the block takes one argument, which is named "f".
In Python, this would be:
def File_new (filename, func_to_call):
try:
file_obj = open (filename, 'w')
func_to_call (file_obj)
finally:
file_obj.close()
def block_function (f): # i.e. this is what that |f| means...
f.write ("Hello, world!\n")
File_new ("test.dat", block_function)
The real difference here is that the file object "f" is closed upon
exit from File_new, even if block_function stashed a reference to "f"
somewhere else. That's probably not that important; this is a case
where Python's reference-counting does the right thing, and where
garbage collection has a slightly harder time.
> ( Maybe it's those extra non alphanumeric operator looking chars
> that make folks think of Perl -- even if they are used differently,
> to someone that doesn't read Ruby, it looks like more "line noise".)
Parts of it are icky, but it's not as bad as it seems at first glance.
The '$' only appears on global variables, while '@' is a shortcut for
"self.", which I don't mind having. "sqrt(@b**2 - 4*@a*@c)" is a bit
more readable for me than "sqrt(self.b**2 - 4*self.a*self.c)".
Anyway, I like Ruby. I don't like it enough to recode all of my
existing python modules in Ruby, but I do like it enough to keep
playing with it.
--
Johann Hibschman johann at physics.berkeley.edu
More information about the Python-list
mailing list