1

I'm trying to run the following but i keep getting a

"NoMethodError: undefined method `bar=' for #/Foo:0x86f69ba/"

    class Class
    def my_attr_accessor(name)

    attr_name = name.to_s  
    attr_reader name       
    attr_reader "my_"+name 
    class_eval %Q"def #{name}=(val)
                 @#{name}=val
                 end"
      end

    end

I'm actually using something similar to this post: Ruby - Using class_eval to define methods

EDITED: you're quite right I made the change to reflect this.. thanks a lot.

Community
  • 1
  • 1
Klam
  • 161
  • 1
  • 15

1 Answers1

3

class_eval is a method, but you are assigning a variable here (class_eval =)

Jim Deville
  • 10,632
  • 1
  • 37
  • 47
  • you should also use `define_method` instead of a `class_eval` – Jim Deville Jan 20 '13 at 07:45
  • Could you please elaborate as to why? I'd be interested to know the reasoning behind it. – Klam Jan 20 '13 at 15:26
  • 1
    @user1994202 evaluating strings instead of blocks is 1) computationally expensive, 2) chronically unsafe -- it's too easy to end up interpolating untrusted data into the string eval allowing external attackers the ability to execute arbitrary code in your process. – dbenhur Jan 21 '13 at 00:47