Class name methods

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?
:slight_smile:

j`ey
http://www.eachmapinject.com
Ergh C(http://code.eachmapinject.com/mountain_signature.c):

char n=’#’,f=’ β€˜;main(){char a[]={f,f,f,f,f,’\0’};
printf("\e[2J\e[H");char *t=a;int i=0,c=i;end:for(
;(c==0?i++<6:i–>0);){c?*t–=f:(*t++=n);printf("%s
\n",a);usleep(200000);};if(c==0){i=6;c=1;goto end;}}

Joey schrieb:

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs