简体   繁体   中英

Ruby issue with class_eval

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.

class_eval是一个方法,但是你在这里分配一个变量( class_eval =

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM