On 11/12/09, Dhaval <dhavalvthaker / gmail.com> wrote:
> hi, i wrote a small code to check metaprogramming in ruby..what am i
> doing wrong..can you pl help?
>
> class Testvar
>  attr_accessor :a1
>  def initialize
>  @a1='init'
> end
> end
>
> var =":a1"
>
> t = Testvar.new
> if t.respond_to?("#{var}")
>  p 'test 1- pass'
> end
> if t.instance_variable_set var , 'abc'
>  p 'test 2-pass'
> end
> if t.send var , 'def'
>  p 'test 3-pass'
> end

What did you expect it to do, and what did it actually do?
I'm guessing that it blows up at instance_variable_set, because a1 is
not a valid ivar name. Try with an @ instead:

  t.instance_variable_set("@#{var}", 'abc')