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: meta, programming, Ruby, visiblity.
Leave a Comment
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