Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
187 views
in Technique[技术] by (71.8m points)

How to insert a new symbol into the instance of Class: BinData::Record in Ruby

I am newbie in ruby. I have a instance of a Class: BinData::Record https://www.rubydoc.info/github/dmendel/bindata/BinData/Record

class Message < BinData::Record
        endian  :big
        uint32 :header  
        uint16 :name
end

meesage is the instance of this class

message = Message.new
message.read('abcdefabcdef')

I want to add a new symbol into the instance, I tried below code

message.age = 10

But it doesn't work.

Could you please tell how to insert a new symbol into the instance of Class: BinData::Record?

question from:https://stackoverflow.com/questions/65948056/how-to-insert-a-new-symbol-into-the-instance-of-class-bindatarecord-in-ruby

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It's better add new attributes to Class definition to use it:

class Message < BinData::Record
  uint32 :age # Add :age
end

There's no easy way to add new symbols to a instance.

If you have to do it, you could check this https://stackoverflow.com/a/27701042/15097028


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...