[#1816] Ruby 1.5.3 under Tru64 (Alpha)? — Clemens Hintze <clemens.hintze@...>

Hi all,

17 messages 2000/03/14

[#1989] English Ruby/Gtk Tutorial? — schneik@...

18 messages 2000/03/17

[#2241] setter() for local variables — ts <decoux@...>

18 messages 2000/03/29

[ruby-talk:01907] Re: I'm stumped...!

From: Dave Thomas <Dave@...>
Date: 2000-03-16 17:27:40 UTC
List: ruby-talk #1907
"David Douthitt" <DDouthitt@cuna.com> writes:

> class Oratab < ConfigFile
>    def open
>       super(oratab)
>    end
...

> #./ruby.functions:43:in `open': wrong # of arguments (0 for 1) (ArgumentError)
> #       from ./ruby.functions:43
> #Oratab.open.each_line { |line|
> #   line.chomp!;
> #   print line, "\n";
> #   }

You're calling 'open' as a class method (or what the documentation
calls a singleton method), but you defined it as an instance method.

You could change the definition to

  class OraTab
     def OraTab.open
       super(oratab)
     end

and you'd then bump in to the next problem. 'oratab' is actually a
local variable in the outer scope, an so is not available within
OraTab. If you run this code, you'd get an error. So... you need to
make 'oratab' globally accessible, either by putting a $ in front of
its name, or by making it a constant (ORATAB).


> #./ruby.functions:57: undefined method `log' for Instance:Class (NameError)
> #Instance.log("test", "info");

Again, you defined it as an instance method, but are calling it as a
class method.

> #./ruby.functions:51: undefined method `instances' for Oratab:Class (NameError)
> #Oratab.instances { |inst|
> #   print inst, " ";
> #   }

And again ;-)


To recap:

A class method (or what some of the documentation calls a singleton
method of a class) can be called at any time, and does not require an
object of that class as a receiver.

An instance method can only be called if you have an object of the
appropriate class.


   class Silly

      def initialize(val)
        @instance_variable = val
      end

      def Silly.sayHello
         puts "hello"
      end

      def sayValue
         print "value = #{@instance_variable}\n"
      end
   end

Given this definition, you can call

   Silly.sayHello

but to call 'sayValue' you need an instance (an object)

   obj = Silly.new('wombat')
   obj.sayValue


Regards


Dave

In This Thread