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

B

Bruce F.

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
 
B

Bruce F.

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
 
A

andrew mcelroy

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. =A0How are they executed? =A0I assume that there's an impli= cit
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). =A0But, what method of that class i= s
it in? =A0And, 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
 
B

Brian Candler

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.
 
B

Bruce F.

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
 
B

Bruce F.

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!
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

Andrew Mcelroy wrote in post #959869:


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!
There should be a devkit for Rubyists on Windows who need to build gems.

Looks like you can get it from here rubyinstaller.org/downloads

If you have difficulty, there appear to be more detailed installation
instructions at
https://github.com/oneclick/rubyinstaller/wiki/Development-Kit as well as
some explanation for the reasoning behind the DevKit, and workarounds for
various issues you might have.
 
A

Andrew Wagner

[Note: parts of this message were removed to make it a legal post.]

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:in
`method_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:in
`configure'
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:in
`initialize'
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:in
`initialize'
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:in
`see_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 `<main>'
 
A

Andrew Wagner

[Note: parts of this message were removed to make it a legal post.]

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top