Jon Raphaelson
4/8/2005 5:25:00 AM
Curt Sampson wrote:
<snip>
>
> For example: explain the results of these two programs to me:
>
> ----------
> def do_me_harder
> puts "Oh yes!"
> end
>
> puts do_me_harder
> ----------
> module Foo
>
> def do_me_harder
> puts "Oh yes!"
> end
>
> puts do_me_harder
> end
> ----------
>
> cjs
Both of those will produce the output
Oh yes!
nil
which is absolutly correct. I'm not sure what the problem with those is.
If it is that you are confused about the nil, that's because puts
returns nil, and you are puts'ing the result of do_me_harder, which is
the result of the last expression in do_me_harder, which is a puts and
hence nil.
If the confusion is that the program wrapped in a module prints
something, that is because, unlike other programming languages where the
class/module defintions are read but not executed, in ruby those
definitions are executed. This is really useful, for example this is the
way alias works and mixins I believe (but I don't know for sure, so if
I'm worng sorry), as well as most domain specific language stuff.