method visibility VS accessibility

Discussion in 'Ruby' started by Thai Le, Feb 4, 2007.

  1. Thai Le

    Thai Le Guest

    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

    --
    Posted via http://www.ruby-forum.com/.
     
    Thai Le, Feb 4, 2007
    #1
    1. Advertising

  2. 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
     
    Robert Klemme, Feb 4, 2007
    #2
    1. Advertising

  3. Thai Le

    Kalman Noel Guest

    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
     
    Kalman Noel, Feb 5, 2007
    #3
  4. On 05.02.2007 11:58, Kalman Noel wrote:
    > 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.


    Correct. There is a "class." missing.

    robert
     
    Robert Klemme, Feb 5, 2007
    #4
  5. Thai Le

    Thai Le Guest

    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?

    --
    Posted via http://www.ruby-forum.com/.
     
    Thai Le, Feb 6, 2007
    #5
  6. Thai Le

    gga Guest

    On 6 feb, 16:22, Thai Le <> 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
     
    gga, Feb 7, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steven T. Hatton
    Replies:
    1
    Views:
    429
    Steven T. Hatton
    Dec 13, 2006
  2. Jay Foad
    Replies:
    5
    Views:
    336
  3. Bob Aman

    method visibility

    Bob Aman, May 28, 2005, in forum: Ruby
    Replies:
    3
    Views:
    125
    Logan Capaldo
    May 29, 2005
  4. Replies:
    0
    Views:
    104
  5. lianliming

    Private visibility for class method

    lianliming, Nov 2, 2008, in forum: Ruby
    Replies:
    1
    Views:
    101
    Rados³aw Bu³at
    Nov 3, 2008
Loading...

Share This Page