[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Class name methods

Pit Capitain

7/12/2006 9:49:00 AM

Joey schrieb:
> Hey Ruby-Talk,
> I've got this:
> class H
>
> end
>
> def H(a)
> p a
> end
> H H
>
> I was wondering how Ruby handles this? I know it works, but *how*?

Ruby has different namespaces for methods and constants, so you can
create both a constant "H" and a method "H". When parsing the expression

H H

Ruby notices that the first "H" is followed by a parameter, so Ruby
chooses the *method* "H". The second "H" doesn't have a parameter, so
Ruby chooses the *constant* "H".

BTW, you can also do the same with methods and local variables:

v = "v"

def v(a)
p a
end

v v # => "v"

Note also the following error messages:

x y # => undefined local variable or method `y'

and

y = 1
x y # => undefined method `x'

Here you see that "x" can only be a method, whereas "y" could be a local
variable or a method.

Regards,
Pit