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

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -