Visibility of a method

July 3, 2008

By default methods within a class in Ruby is PUBLIC. But as we know, ruby supports meta programming and hence a new/updated code can be generated on fly during the runtime.
here is a way to make the public class methods to private.

class Myclass
  def mymethod
    puts "I am public and wishing to be private!"
  end
  private :mymethod
  # OR also can be written as
  # private_class_method :mymethod
end

obj = Myclass.new
obj.mymethod
# private method `mymethod' called for # (NoMethodError)

Similary public :mymethod or public_class_method :mymethod

Entry Filed under: Ruby. Tags: , , , .

Leave a Comment

hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Categories

posts[:recent]

episodes[:recycled]

@@name = PRAYAS

Step down at my blog with your ideas,comments,suggestions on Ruby,RoR,Ajax or Web2.0.You may reach me at
infostall@gmail.com

find_by_tags

Links

visitors[:since_Mar'08]

free web counter

Spam Blocked

Feeds

Meta

RSS Prayas here