D
Daniel Schüle
Hello all,
I am reading this page
http://www.rubyist.net/~slagell/ruby/accesscontrol.html
especially the "fish".square example
I am puzzled by different behaviours of ruby itself and the irb
irb(main):101:0> def square x
irb(main):102:1> x*x
irb(main):103:1> end
=> nil
irb(main):105:0> [].square 12
=> 144
irb(main):106:0> "fish".square 12
=> 144
irb(main):112:0> "fish".public_methods.grep /square/
=> ["square"]
irb(main):113:0> "fish".private_methods.grep /square/
=> []
I can square my fishes and arrays
but a ruby program
#!/usr/bin/env ruby
def square x
x*x
end
puts square 10
#puts "fish".square # error, cant square fishes
puts Object.private_methods.grep /square/ # yes it's there
puts Object.public_methods.grep /square/ # nope
as you can see, irb makes methods public and ruby private
what rationale is behind this behaviour?
is this something that will be "fixed" in future
or is deliberately dicision/feature
Regards, Daniel
I am reading this page
http://www.rubyist.net/~slagell/ruby/accesscontrol.html
especially the "fish".square example
I am puzzled by different behaviours of ruby itself and the irb
irb(main):101:0> def square x
irb(main):102:1> x*x
irb(main):103:1> end
=> nil
irb(main):105:0> [].square 12
=> 144
irb(main):106:0> "fish".square 12
=> 144
irb(main):112:0> "fish".public_methods.grep /square/
=> ["square"]
irb(main):113:0> "fish".private_methods.grep /square/
=> []
I can square my fishes and arrays
but a ruby program
#!/usr/bin/env ruby
def square x
x*x
end
puts square 10
#puts "fish".square # error, cant square fishes
puts Object.private_methods.grep /square/ # yes it's there
puts Object.public_methods.grep /square/ # nope
as you can see, irb makes methods public and ruby private
what rationale is behind this behaviour?
is this something that will be "fixed" in future
or is deliberately dicision/feature
Regards, Daniel