Pit Capitain
10/16/2006 7:39:00 PM
John Ky schrieb:
> Why is it that when I can do this:
>
>> include Math
>> puts sqrt(169)
>
> I can't do this?:
>
>> class X
>> include Math
>> puts sqrt(169)
>> end
Rick has already written about the differences between class scope and
toplevel scope. (Are you reading this, Tom?)
With #include you can call the Math methods in instance methods:
class X
include Math
def m
puts sqrt(169)
end
end
X.new.m # => 13.0
If you want to call the methods in class scope, you have to use #extend:
class X
extend Math
puts sqrt(169) # => 13.0
end
Regards,
Pit