ruby - Assigning multiple values to setter at once: self.x = (y, z) results in a syntax error -
i'm trying define class method using 2 arguments - title , author. when seek pass arguments i'm getting argument error
syntax error, unexpected ',', expecting ')' book.set_title_and_author= ("ender's game", "orson scott card")
class book def set_title_and_author= (title, author) @title = title @author = author end def description "#{@title}was written #{@author}" end end book = book.new book.set_title_and_author= ("ender's game", "orson scott card) p book.description am not allowed pass more 1 argument in setter method or there else i'm missing?
you indeed can't pass more 1 argument method ends in =. setter method doesn't need end in =, though, naturally: can set_title_and_author(title, author).
another alternative have method take array:
def set_title_and_author= (title_and_author) @title, @author = title_and_author end #... book.set_title_and_author= ["ender's game", "orson scott card"] if latter, stylistically i'd recommend removing set , calling method title_and_author=. set redundant =.
ruby syntax setter getter-setter
No comments:
Post a Comment