?

Log in

No account? Create an account

ruby variable / function confusion

« previous entry | next entry »
May. 10th, 2006 | 10:32 pm
music: Prince - Musicology - Musicology

I'm pretty new to ruby, but this seems damn confusing, when variables and functions with the same name can exist in the same frame, and not collide, but depending on which exists, that changes what gets called. Or is this the same as common lisp, and I'm just spoiled by scheme?
irb(main):004:0> def poop(arg=0)
irb(main):005:1> return arg + 12
irb(main):006:1> end
=> nil
irb(main):007:0> poop
=> 12
irb(main):008:0> poop 23
=> 35
irb(main):009:0> poop = 14
=> 14
irb(main):010:0> poop
=> 14
irb(main):011:0> poop()
=> 12
irb(main):012:0> undef poop
=> nil
irb(main):013:0> poop
=> 14
irb(main):014:0> poop()
NoMethodError: undefined method `poop' for main:Object
from (irb):14
from :0

| Leave a comment | Share

Comments {5}

geekgirlxrds

(no subject)

from: geekgirlxrds
date: May. 11th, 2006 05:41 am (UTC)

poo balls happen!

Reply | Thread

(no subject)

from: zuckussi
date: May. 11th, 2006 11:29 am (UTC)

Is this Alex? Add me as a friend. 'Tis David. You were spotted in the anonymous thread. I was the survey starter for the "How many people are here?"

Reply | Thread

Alex P.

(no subject)

from: wetzel
date: May. 11th, 2006 04:15 pm (UTC)

aye, 'tis

Reply | Parent | Thread

dersuuzala

(no subject)

from: dersuuzala
date: May. 12th, 2006 09:03 am (UTC)

this looks pretty consistent to me. separate symbol tables for methods and variables, and once the same entry exists in both, the alias for calling the method gets shadowed by the variable.

after you build a compiler in 164 this stuff all makes sense.

Reply | Thread

Alex P.

(no subject)

from: wetzel
date: May. 12th, 2006 05:36 pm (UTC)

i guess so . . . it's just when scheme has first class functions where a variable can be a function, it seems a little odd to kind of get half that through syntax without actually doing anything to the language.

Well, I guess the syntax is a big part of the language, but it makes sense in my head.

Hello, by the way. I didn't know you were a livejournaler as well as a flickr-er.

Reply | Parent | Thread