Send to access private members

I'm a confused about why the following code works:

class Foo
   private
   def hello
      puts "hello"
   end
end
f = Foo.new
f.send(:hello)

whereas
f.hello will obviously fail.

I have tried this with private member setting as well and it also works.
Is send supposed to operate this way? The rdoc for it don't seem to
mention this. Thanks!

···

--
Posted via http://www.ruby-forum.com/.

Hi --

···

On Sat, 6 Jan 2007, David Weldon wrote:

I'm a confused about why the following code works:

class Foo
  private
  def hello
     puts "hello"
  end
end
f = Foo.new
f.send(:hello)

whereas
f.hello will obviously fail.

I have tried this with private member setting as well and it also works.
Is send supposed to operate this way? The rdoc for it don't seem to
mention this. Thanks!

Yes, send ignores the private status. In 2.0 there will be a version
of send that doesn't and one that does. It's not certain what they'll
be called (but if you have nothing to do for the next week or two you
can look at the discussions about it :slight_smile:

David

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black\)
    (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf\)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)