ruby - Using Struct in Class Definition -
i have found rare(to me) line of code in 1 of ours old repositories. following code aws part of poc application simple note taking app. nevertheless, these 2 lines in model folder surprised me:
class note < struct.new(:title, :due_to, :body, :self_url, :image_url) end
i looking technique while , haven't found valuable source of information implementaiton.
can me better understand this? think kind usage of struct creating new object inherits struct. don't see benefits here.
struct.new(:title, :due_to, :body, :self_url, :image_url) #=> #<class:0x007fed5b1859c0>
check out, struct.new
returns class. class can create instances getters , setters named fields. and, of course, class can inherited from.
so odd way of defining list of instance properties in class declaration.
which means funcationally same as:
class note attr_accessor :title, :due_to, :body, :self_url, :image_url end
Comments
Post a Comment