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

Entry filed under: Ruby. Tags: , , , .

Regular expression Large development@Rails

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


Categories

posts[:recent]

@@name = PRAYAS

This blog has more intention for my self reference@24×7. But if some search engine crawler directs you to my page, believe I may learn form your comments or feedback. Hence feel free to comment good or bad!
prayas2010@gmail.com

visitors[:since_Mar'08]

free web counter

Prayas@Nikon

A colored life

Chhweet Kidss

An independent world

More Photos

Feeds


Follow

Get every new post delivered to your Inbox.