Parser/execution question

I understand that even though you can define variables and methods at the
top level of Ruby like:

a = “hello”

def test
p "test"
end

these are actually at ‘Object’ level. Does this mean that there is scope
open for ‘Object’ at the very top level?

Is the above the same as:

class Object
a = "hello"
def test
p "test"
end
end

How does executing code for the first example differ from executing the code
within a class? Does it even?

At the top level, where is the code being executed?

tia,

···


Justin Johnson.

these are actually at 'Object' level. Does this mean that there is scope
open for 'Object' at the very top level?

At top level
   * self is an instance of Object
   * class is Object

pigeon% ruby -e 'p self, self.type'
main
Object
pigeon%

methods defined at top level are private methods.

class Object
  a = "hello"
  def test
    p "test"
  end
end

You have defined a public method of Object.

Be carefull with Object#test because ruby define the method Kernel#test

Guy Decoux

Ok.

If you have:

a = “hello”

which is a local variable, what is the variable local to?

···


Justin Johnson.

“ts” decoux@moulon.inra.fr wrote in message
news:200207171618.g6HGIjx07844@moulon.inra.fr

these are actually at ‘Object’ level. Does this mean that there is
scope
open for ‘Object’ at the very top level?

At top level

  • self is an instance of Object
  • class is Object

pigeon% ruby -e ‘p self, self.type’
main
Object
pigeon%

methods defined at top level are private methods.

class Object
a = “hello”
def test
p “test”
end
end

You have defined a public method of Object.

Be carefull with Object#test because ruby define the method Kernel#test

Guy Decoux

which is a local variable, what is the variable local to?

local to the current scope. A scope is created at top level, like a scope
is created in the body of a class.

Guy Decoux