Forum: Ruby How to invoke a run method???

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Eric Armstrong (Guest)
on 2006-06-13 04:37
(Received via mailing list)
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/...

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)
Daniel Harple (Guest)
on 2006-06-13 04:58
(Received via mailing list)
On Jun 13, 2006, at 4:37 AM, Eric Armstrong 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
unknown (Guest)
on 2006-06-13 05:02
(Received via mailing list)
On Tue, 13 Jun 2006, Eric Armstrong 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
Eric Armstrong (Guest)
on 2006-06-13 21:24
(Received via mailing list)
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...
:_)
Eric Armstrong (Guest)
on 2006-06-13 21:27
(Received via mailing list)
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
Eric Armstrong (Guest)
on 2006-06-14 01:25
(Received via mailing list)
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?
This topic is locked and can not be replied to.