This is pretty ridiculous, but I have tried
virtually every combination of class and module
definitions that I can think of, all without
success:
I started with an old Post by Ara Howard:
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/193039
module Foo
class Main
…
end
end
Foo::Main.run(ARGV, ENV) if FILE == $0
I construed that post to mean that this work:
module Foo
class Main
def run
puts “Running”
end
end
end
Foo::Main.run() if FILE == $0
But the result was:
undefined method `run’ for Foo::Main:Class
Then I found the ERB program at
http://stuff.mit.edu/afs/sipb/project/ruby-lang/bin/erb
That one reverses class & module:
class ERB
module Main
def run
puts “Running”
end
end
end
Foo::Main.run if FILE == $0
But I got a similar result:
undefined method `run’ for Foo::Main:Module
So I tried with the class definition by itself, and
with the module definition by itself. Nothing worked.
What the heck is wrong with this picture?
(thanks in advance)
On Jun 13, 2006, at 4:37 AM, Eric A. wrote:
But the result was:
undefined method `run’ for Foo::Main:Class
#run is an instance method. You could make it a class method:
module Foo
class Main
def self.run
puts “Running”
end
end
end
Foo::Main.run if FILE == $PROGRAM_NAME
Or instigate an object of class Foo::Main:
Foo::Main.new.run if FILE == $PROGRAM_NAME
– Daniel
On Tue, 13 Jun 2006, Eric A. wrote:
…
end
end
Foo::Main.run(ARGV, ENV) if FILE == $0
Foo::Main.new.run(ARGV, ENV) if FILE == $0
^^^
^^^
^^^
Then I found the ERB program at
http://stuff.mit.edu/afs/sipb/project/ruby-lang/bin/erb
That one reverses class & module:
class ERB
module Main
def run
def self.run
puts "Running"
end
end
end
Foo::Main.run if FILE == $0
What the heck is wrong with this picture?
(thanks in advance)
in both situations you are reversing the notion of class/module and
instance
methods. in the first case you call a class method when it should be an
instance method, in the second you define an instance methods and then
try to
call a class method.
module M
def self.foo
‘this is a class/module method’
end
def bar
‘this is an instance method’
end
end
we can do
M.foo
class C
include M
end
C.new.bar
alternatively we can have
class C
def self.foo
‘this is a class/module method’
end
def bar
‘this is an instance method’
end
end
and do
C.foo
C.new.bar
make sense?
-a
Thanks to you, too, Daniel.
BTW: I notice you use $PROGRAM_NAME, instead of $0.
I take that is a recently added variable? My five
Ruby books don’t seem to mention it.
–signed, Hopelessly addicted to paper
Ok. Got it. Many thanks. The use of “self” threw
for a moment. Always thought of that as an object.
But on reflection, it’s clear that in a class definition,
it would refer to the class object.
On a related subject, what’s going on with that
ERB program? Weirdest thing I ever saw. Why would
you define a Module inside of a Class, instead of
the other way 'round?
Thanks for the additional explanation. I’ll
mull it over. The difference between instance
and class methods is crystal clear in my java
mind, but my ruby needs more educating.
To make matter works, I /tried/ Foo::Main.new.run
–but I must have inadvertently done so when
run was a class method.
Have to fire up the gray cells…
:_)