Code in a class but not in a method -- please explain!

I’m a newcomer to Ruby, and I’m confused about what executable
statements which are in a class definition, but not explicitly in a
method, mean. How are they executed? I assume that there’s an implicit
method that they define; is that right?

The most obvious example is the one-line program:
puts “Hello, World!”

Since there’s no explicit class definition, I understand that it’s in an
automatically generated class (main). But, what method of that class is
it in? And, if it were an explicit class, how would I execute it?
(Apparently it’s executed automatically if it’s in main.)

Thanks,
Bruce

http://pine.fm/LearnToProgram/

http://pine.fm/LearnToProgram/Pretty good resource. Check it out.

Thanks, Dan. I appreciate the reference you gave me, but I’m still
stumped after looking through it.

I’ve looked through it in what seemed the obvious places, but I don’t
see the the answer to my question. I think the problem may be that I
don’t know some terminology, so my attempts to search aren’t working.

Although I’m a newcomer to Ruby, I’m an experienced OO programmer, using
C++, Java, and many other languages. (I started programming in 1971.)
I’ve read most of “Well Grounded Rubyist” by David Black, and of course
Collingbourne’s “The Book of Ruby”. I haven’t been able to find my
answer in them, either. (That doesn’t mean that it’s not there – but I
haven’t found it, and if I’ve read the answer it didn’t register.)

Bruce

On Sat, Nov 6, 2010 at 10:22 PM, Bruce F. [email protected] wrote:

it in? And, if it were an explicit class, how would I execute it?
(Apparently it’s executed automatically if it’s in main.)

Checkout DrX http://drx.rubyforge.org/
It might visually show you what’s going on.

Andrew

Bruce F. wrote in post #959861:

I’m a newcomer to Ruby, and I’m confused about what executable
statements which are in a class definition, but not explicitly in a
method, mean. How are they executed? I assume that there’s an implicit
method that they define; is that right?

No.

When you load a ruby script, firstly it is parsed into an internal
representation, and then that representation is stepped through and
executed.

Even the definition of a class, and the creation of methods, is done
dynamically at runtime. For example:

a = gets.chomp
b = gets.chomp
if a == b
class Foo
end
else
class Bar
end
end

In this program, if you enter two strings the same then a class Foo is
created (but not Bar); if they are different, then Bar is created (but
not Foo).

Most of the time, class definitions are not inside any conditional, but
they are still dynamic. This means they don’t exist until the class
definition code has run.

a = Foo.new # runtime error: Foo does not exist
class Foo
end
b = Foo.new # this is OK

Now, consider that ‘def’ is also a runtime action, which when it
executes, defines (or redefines) a method. The same issue applies:

wibble(123) # runtime error: No such method
def wibble(a)
puts a
end
wibble(456) # this is OK

Put the two together, and class … def … end … end is just code
which is executed. class Foo only does two things: it creates Foo if it
doesn’t already exist, and it sets the class context to which any
enclosed ‘def’ statements apply.

So now consider this:

puts “Hello world!” # this is executed before Foo created
class Foo
puts “About to define bar” # Foo created, bar hasn’t yet
def bar
puts “Yay!”
end
puts “Finished defining bar”
end
Foo.new.bar

Can you work out what this will print when you run it? When you’ve had a
think, run it to check.

HTH,

Brian.

Andrew Mcelroy wrote in post #959869:

Checkout DrX http://drx.rubyforge.org/
It might visually show you what’s going on.

This looks like a very interesting and useful tool. I got an answer
before I succeeded in installing it (since it seems that I need a C
development package on my system to install it – I’m running Windows
7), but I’m going to proceed with installing anyway.

Thanks!

On Sun, Nov 7, 2010 at 6:50 AM, Bruce F. [email protected] wrote:

Thanks!


Posted via http://www.ruby-forum.com/.

There should be a devkit for Rubyists on Windows who need to build gems.

Looks like you can get it from here Downloads

If you have difficulty, there appear to be more detailed installation
instructions at
Development Kit · oneclick/rubyinstaller Wiki · GitHub as well
as
some explanation for the reasoning behind the DevKit, and workarounds
for
various issues you might have.

There still seem to be problems even once you get past that. I installed
tcl
and tk_as_gem, but when I try to run the sample code on drx’s website, i
get
this error:

C:/Ruby/lib/ruby/gems/1.9.1/gems/tk_as_gem-0.1.0/lib/tk.rb:4807:in
rescue in method_missing': unknown optio 'to_ary' for #<TkImageMap::ImageMap:0x28330b0 @path=".w00002"> (deleted widget?) (NameError) from C:/Ruby/lib/ruby/gems/1.9.1/gems/tk_as_gem-0.1.0/lib/tk.rb:4803:inmethod_missing’
from
C:/Ruby/lib/ruby/gems/1.9.1/gems/tk_as_gem-0.1.0/lib/tk/pack.rb:42:in
flatten' from C:/Ruby/lib/ruby/gems/1.9.1/gems/tk_as_gem-0.1.0/lib/tk/pack.rb:42:inconfigure’
from
C:/Ruby/lib/ruby/gems/1.9.1/gems/tk_as_gem-0.1.0/lib/tk.rb:5048:in
pack' from C:/Ruby/lib/ruby/gems/1.9.1/gems/drx-0.4.5/lib/drx/tk/imagemap.rb:54:ininitialize’
from
C:/Ruby/lib/ruby/gems/1.9.1/gems/drx-0.4.5/lib/drx/tk/app.rb:75:in new' from C:/Ruby/lib/ruby/gems/1.9.1/gems/drx-0.4.5/lib/drx/tk/app.rb:75:ininitialize’
from C:/Ruby/lib/ruby/gems/1.9.1/gems/drx-0.4.5/lib/drx.rb:12:in
new' from C:/Ruby/lib/ruby/gems/1.9.1/gems/drx-0.4.5/lib/drx.rb:12:insee_using_tk’
from C:/Ruby/lib/ruby/gems/1.9.1/gems/drx-0.4.5/lib/drx.rb:31:in
see' from scratch.rb:4:in

Brian – Thanks!

That’s exactly what I wanted to understand. I was locked into the wrong
mindset (too many compiled languages!), and was looking for something
that essentially wasn’t there. I get it now.

Thanks again!
Bruce

Actually, I swapped out tk_as_gem, swapped in tk-win, and it seems to
work
fine. Go figure.