Logan Capaldo
3/26/2006 11:05:00 PM
On Mar 26, 2006, at 5:43 PM, john_sips_tea@yahoo.com wrote:
> I'll have to think about that. I don't see the connection between
> strings and method lookup. In Python code, you write a name(),
> and python works its way up the inheritance tree looking for
> the function definition. You can't call a method like:
>
> def my_func():
> print "hi"
> foo = "my_func"
> foo() # Trying to call my_func, but it fails.
>
> so I don't see the string/method-call connection you're
> referring to...
As a little aside, python lets you do:
def z(x):
print x
a = z
a("Hello")
Hello
yes?
well ruby as you know, makes parens optional in method calls, what
this means is you can't directly assign methods around. So in ruby
you can do this:
send("z", "Hello")
or this
send(:z, "Hello")
In the first case, z has to be hashed to its numeric symbol code at
run time. This means somewhere inside the send method, its doing
"z".to_sym. When you use :z, the hash is computed at compile time,
and you're basically passing send a number, let's say 9.
send(9, "Hello")
In ruby Symbols don't have the extra level of indirection that
strings do. "z" is a pointer to the string object that contains the
character 'z'. :z is the number 9. (well not really, but it is *a*
number).
This is a reason for using symbols as hash keys, if you never display
the keys for instance. Its a number so equality comparsions become
trivial and hashing operations also become cheaper (I imagine so
anyway).
Using symbols basically saves you one level of runtime lookup.