Ruby: how the interpreter parses your code

I wasn’t sure exactly how the Ruby code I’d written was being interpreted.

For example, 

class TestClass

def initialize(x = 1)

@x = x

end

puts “inside the class”

end

a = TestClass.new(5)

puts “declared an instance”

Now, does this print “inside the class” or “declared an instance” first?

The answer is “inside the class”.

See also this post describing using Ripper to parse code:

http://www.rubyinside.com/using-ripper-to-see-how-ruby-is-parsing-your-code-5270.html 

Leave a Reply

Your email address will not be published. Required fields are marked *