Hi list.
I’ve recently learned that irb and ruby (ruby interpreter) behave
differently often
and that irb is not running on ruby interpreter.
As an example,
[mkseo@uranus src]$ irb
irb(main):001:0> def foo
irb(main):002:1> puts “foo”
irb(main):003:1> end
=> nil
irb(main):004:0> class Bar; end
=> nil
irb(main):005:0> Bar.new.foo
foo
=> nil
irb(main):006:0> quit
irb let me call foo which is non-sense.
[mkseo@uranus src]$ cat > test.rb
def foo
puts “foo”
end
class Bar; end
Bar.new.foo
[mkseo@uranus src]$ ruby test.rb
test.rb:5: private method `foo’ called for #Bar:0x2aaaaab00cc8
(NoMethodError)
[mkseo@uranus src]$
Ruby interpreter fails as expected.
So, here’s my question. Why is irb running on its own interpreter(or
simulator or whatever)?
Sincerely,
Minkoo S.
On 6/30/06, Minkoo S. [email protected] wrote:
I’ve recently learned that irb and ruby (ruby interpreter) behave
differently often and that irb is not running on ruby interpreter.
You have learned incorrectly.
irb behaves differently than Ruby might otherwise behave because all
of the code entered is eval-ed when it’s in a “complete” state for the
Ruby interpreter to use.
irb most certainly runs on top of Ruby. But there occasional
permissions/visibility differences (pretty much the only differences
that matter) which will bite someone’s expectations.
-austin
Hi austin.
On 7/1/06, Austin Z. [email protected] wrote:
irb behaves differently than Ruby might otherwise behave because all
of the code entered is eval-ed when it’s in a “complete” state for the
Ruby interpreter to use.
I’m afraid that I don’t understand “all of the code entered is eval-ed
when it’s in a “complete” state for the Ruby interpreter to use” means.
What do you mean by ‘evaled’ and ‘complete state’? And Why are the
visibility differences happening?
Sincerely,
Minkoo S.
On Jun 30, 2006, at 7:10 PM, Minkoo S. wrote:
irb(main):002:1> puts “foo”
Ruby interpreter fails as expected.
So, here’s my question. Why is irb running on its own interpreter(or
simulator or whatever)?
Sincerely,
Minkoo S.
When you def a method in irb outside of any class definition it
actually gets added to Object i believe. Since the top level of irb
outside of any class definitions is essentially running inside of
Object. So that is why you can call foo from inside of class Bar
since class Bar inherits from Object by default.
ez $ irb
irb(main):001:0> self.class
=> Object
-Ezra
Hi Ezra.
You’ve got it wrong. The point is that though a method that is defined
outside of any class becomes automatically part of Object, it can’t be
called with exact receiver because the method is private.
In other words,
foo is private method of Object
def foo
…
end
class Bar; end
This is okay, because I’m calling self.foo and we do not
need to state self as receiver.
foo
This is completely wrong. Private method can’t be called
with exact receiver.
Bar.new.foo
Hope this helps.
Sincerely,
Minkoo S.
Hi-
On Jun 30, 2006, at 9:19 PM, Minkoo S. wrote:
…
Bar.new.foo
Hope this helps.
Sincerely,
Minkoo S.
My irb disagrees with you
irb(main):012:0> def foo
irb(main):013:1> puts ‘foo’
irb(main):014:1> end
=> nil
irb(main):015:0> class Bar
irb(main):016:1> end
=> nil
irb(main):017:0> Bar.new.foo
foo
=> nil
irb(main):018:0> Object.private_methods.grep /foo/
=> []
irb(main):019:0> Object.instance_methods.grep /foo/
=> [“foo”]
irb(main):020:0> Bar.instance_methods.grep /foo/
=> [“foo”]
irb(main):021:0> Bar.private_methods.grep /foo/
=> []
But I understand that in a normal ruby script methods def’ed outside
of a class definition becore private methods of Object but its not
the case in irb.
-Ezra
On Jul 1, 2006, at 12:15 AM, Minkoo S. wrote:
I’m afraid that I don’t understand “all of the code entered is eval-ed
when it’s in a “complete” state for the Ruby interpreter to use”
means.
What do you mean by ‘evaled’ and ‘complete state’? And Why are the
visibility differences happening?
Sincerely,
Minkoo S.
irb is a repl written in ruby which means it uses eval (or
module_eval or instance_eval). Now whether intentionally or not (one
could probably find out by looking at the source), all irb sessions
basically have an implicit
irb(main):000:0> public
at the top.
If you want it to act more like a bog-standard ruby top-level, you
can type private at the start of your irb session. (unfortunately
putting it in your .irbrc won’t work)
On Sat, Jul 01, 2006 at 01:32:54PM +0900, Logan C. wrote:
What do you mean by ‘evaled’ and ‘complete state’? And Why are the
visibility differences happening?
irb is a repl written in ruby which means it uses eval (or
module_eval or instance_eval). […]
As for “complete state”: irb needs a full (“complete”) expression before
it
can run eval(str). It is intelligent enough to keep reading until the
expression can be eval’ed, e.g. if you enter
class Foo
it will ask for input until it sees a matching ‘end’.
Note that irb’s lexer is not perfect so this might fail occasionally.