IT Community - Software Programming, Web Development and Technical Support

How is visibility of methods changed in Ruby?

This is a discussion on How is visibility of methods changed in Ruby? within the Ruby forums, part of the Web Development category; How is visibility of methods changed in Ruby?...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > Ruby

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 07-16-2007, 03:05 AM
vadivelanvaidyanathan vadivelanvaidyanathan is offline
D-Web Genius
 
Join Date: Feb 2007
Posts: 803
vadivelanvaidyanathan is on a distinguished road
Question How is visibility of methods changed in Ruby?

How is visibility of methods changed in Ruby?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-27-2007, 11:33 PM
kingmaker kingmaker is offline
D-Web Genius
 
Join Date: Jun 2007
Posts: 882
kingmaker is on a distinguished road
Send a message via Yahoo to kingmaker
Thumbs up Re: How is visibility of methods changed in Ruby?

public, protected, and private


class Foo
def a; end

# call 'a' with explicit 'self' as receiver
def b; self.a; end

# call 'a' with implicit 'self' as receiver
def c; a; end
end

def safe_send(receiver, method, message)
# can't use 'send' because it bypasses visibility rules
eval "receiver.#{method}"
rescue => e
puts "#{message}: #{e}"
else
puts "#{message}: succeeded"
end

visibility = ARGV.shift || "public"
Foo.send(visibility, :a)

foo = Foo.new
safe_send(foo, :a, "explicit receiver ")
safe_send(foo, :b, "explicit 'self' receiver")
safe_send(foo, :c, "implicit 'self' receiver")

Basically, the script just creates a class “Foo” with three methods: a, which we’ll invoke directly with an explicit, non-self receiver; b, which invokes a with self as receiver, and c, which invokes a with an implicit receiver of self. We’ll use the safe_send method to call each of those methods and log the result.

So, first: the public keyword. In Ruby, public means that the method may be invoked just about any way you please; in technical terms, the receiver of the message may be either explicit (“foo.bar”), self (“self.bar”) or implicit (“bar”).


$ ruby demo.rb public
explicit receiver : succeeded
explicit 'self' receiver: succeeded
implicit 'self' receiver: succeeded


The protected keyword puts a straitjacket around the method. Any method declared protected may only be called if the receiver is self, explicitly or implicitly. (Update: protected methods may actually be called any time the receiver is of the same class as ‘self’...and an explicit self as receiver is just a specific case of that. Modifying the script to demonstrate this condition is left as an exercise for the reader.)


Lastly, the private keyword is the tightest setting of all. A private method cannot be called with an explicit receiver at all, even if that receiver is “self”.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Web service methods? devarajan.v XML and SOAP 1 08-11-2007 01:33 AM
lifecycle methods os JSP lavanya Java Server Pages (JSP) 2 08-09-2007 06:42 AM
What are the authentication modes in SQL Server? How can it be changed? KiruthikaSambandam Database Support 2 08-08-2007 07:57 AM
When should I use constructors, and when should I use other methods? prasath Java Programming 1 07-20-2007 07:42 AM
What are native methods? How do you use them? vadivelanvaidyanathan Java Programming 1 07-17-2007 06:10 AM


All times are GMT -7. The time now is 02:09 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0