I am working on a project and I found an interesting problem. I wanted to use Struct
. I try to use it like this:
> e = Struct.new(:message, :whateve)
=> #<Class:0x007f881dd98b98>
e.message = "something"
NoMethodError: undefined method `message=' for #<Class:0x007f881dd98b98>
from (pry):5:in `__pry__'
It works fine if I add it like this: e = Struct.new(:message, :whateve).new
so I am wondering what is the problem and needs the extra new? I am using ruby 2.2.1p85.
It happens, because Struct.new(:message, :whateve)
just defines a new class:
e = Struct.new(:message, :whateve) # define new class
e.class
#=> Class
e.new #define this class instance
#=> #<struct message=nil, whateve=nil>
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.