Visibility of a method
July 3, 2008 at 7:09 pm Leave a comment
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
Like this:
Be the first to like this post.
Entry filed under: Ruby. Tags: meta, programming, Ruby, visiblity.



Trackback this post | Subscribe to the comments via RSS Feed