ruby confusing -- local variable or instance_method ?

Posted by boblu on Stack Overflow See other posts from Stack Overflow or by boblu
Published on 2010-05-14T09:03:43Z Indexed on 2010/05/14 11:54 UTC
Read the original article Hit count: 165

Filed under:
|

I have the following program.

module C
  def self.included(base)
    base.extend(ClassMethods)

  end

  module ClassMethods
    def test_for
      class_eval <<-DEFINECLASSMETHODS
        def self.my_method(param_a)
          puts "SELF is: #{self.inspect}"
          puts param_a
          puts "#{param_a}"
        end
      DEFINECLASSMETHODS
    end
  end
end

class A
  include C
end

class B < A
  test_for
end

when I run B.new.my_method("aaa"), I got this error

NameError: undefined local variable or method `param_a' for B:Class

I am quite confused.

I define param_a as a local variable in class method my_method,

puts param_a

runs good, and will output the "aaa".

however,

puts "#{param_a}"

output that error.

why?

Can anyone explain this?

© Stack Overflow or respective owner

Related posts about ruby

Related posts about metaprogramming