Forum: Ruby method visibility VS accessibility

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.
315065614a6bbd76345f1969a58a0e08?d=identicon&s=25 Thai Le (lnthai2002)
on 2007-02-04 22:49
Hi guys,
I've done java for a few years and ruby is pretty new to me. In java, a
method has visibility(private, protected, public) and
accessibility(static, non-static); however, in ruby how can I declare
the method: "public static method1"? It seem that the default visibility
of a method is public in ruby, am i correct?
One morething is that java program starts from "public static main()".
What is the entry point to ruby program?
Thanks in advance
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2007-02-04 23:25
(Received via mailing list)
On 04.02.2007 22:49, Thai Le wrote:
> I've done java for a few years and ruby is pretty new to me. In java, a
> method has visibility(private, protected, public) and

Actually there are four of them: you have to add "package" visibility to
the list. :-)

> accessibility(static, non-static); however, in ruby how can I declare
> the method: "public static method1"? It seem that the default visibility
> of a method is public in ruby, am i correct?

Yes.  Strictly speaking there are no static methods in Ruby.  But it has
the concept of singleton methods that are defined for a single instance
only.  And since classes are just ordinary objects you can define
instance methods of class objects and achieve basically the same as with
Java's static methods:

class Foo
   def self.a_class_method
     # ...
   end

   def an_instance_method
     self.class.a_class_method
     self.another_class_method
   end
end

def Foo.another_class_method
   # ...
end

> One morething is that java program starts from "public static main()".
> What is the entry point to ruby program?

The situation is a bit different because Ruby is interpreted.  If you
want, you can view the root script you invoke as the body of main, i.e.,
code is executed top down.  However, part of this execution are class,
method and constant definitions.  Invocation arguments are accessible
via ARGV (and also ARGF which is a special shortcut which will read from
all files in ARGV).  So, strictly speaking there is no equivalent of
Java's and C's "main" but the functionality is there.

Kind regards

  robert
D032dc2ce8e6e785161fb4e3ebb64ba3?d=identicon&s=25 Kalman Noel (Guest)
on 2007-02-05 12:06
(Received via mailing list)
Robert Klemme:
> class Foo
>    def self.a_class_method
>      # ...
>    end
>
>    def an_instance_method
>      self.class.a_class_method
>      self.another_class_method
       ^^^^^^^^^^^^^^^^^^^^^^^^^
>    end
> end
>
> def Foo.another_class_method
>    # ...
> end

It seems that you did not write what you meant in the marked line.

Kalman
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2007-02-05 13:51
(Received via mailing list)
On 05.02.2007 11:58, Kalman Noel wrote:
>>    end
>> end
>>
>> def Foo.another_class_method
>>    # ...
>> end
>
> It seems that you did not write what you meant in the marked line.

Correct.  There is a "class." missing.

  robert
315065614a6bbd76345f1969a58a0e08?d=identicon&s=25 Thai Le (lnthai2002)
on 2007-02-06 20:22
Robert Klemme wrote:
> Yes.  Strictly speaking there are no static methods in Ruby.  But it has
> the concept of singleton methods that are defined for a single instance
> only.  And since classes are just ordinary objects you can define
> instance methods of class objects and achieve basically the same as with
> Java's static methods:
>
> class Foo
>    def self.a_class_method
>      # ...
>    end
>
>    def an_instance_method
>      self.class.a_class_method
>      self.another_class_method
>    end
> end
>
So if i have a class method, I can invoke that method from anywhere by
using class name like: Foo.a_class_method() without declaring public for
that method?
B74f9ac58c7a0b80d877470198e1a472?d=identicon&s=25 gga (Guest)
on 2007-02-07 06:55
(Received via mailing list)
On 6 feb, 16:22, Thai Le <lnthai2...@yahoo.com> wrote:
>
> So if i have a class method, I can invoke that method from anywhere by
> using class name like: Foo.a_class_method() without declaring public for
> that method?

Yes.  You can use either of:

Class::method
or
Class.method
This topic is locked and can not be replied to.