Ruby Project - Book Titles: Is a function like "def title=" a convention?


!Spoiler Warning! If you haven’t completed the Ruby Basics Project of Web Dev 101 yet, you may not want to read this.

I just completed the sixth task of the Ruby Basics Project (05_book_titles) and even though I did know what I had to do (as it was similar to the Simon Says task), I had no idea, what the test wanted, so I looked at the solution.
The problem I had was, that two functions had to be specified - title and title=. Coming from Python, it seems really weird to define a function called title= and invoking it with "@book.title = ‘something’ ".

Is defining a function with an equal sign at the end of a functions name a convention in Ruby?
Why not let the Book-Class take a title-argument and invoking the titleize-function in the initialize-function of the Class?

Maybe I missed this part during the tutorials, but I can’t remember learning about the possibility of creating such a function. I really like this principle, but not knowing about that made the task a little hard for me, even though I knew how to solve the problem.

Thank you!

Edit: I looked through the tutorials again and I didn’t find an explanation, that such a function is possible. Maybe you want to explain this at one point (I’d prefer this) or change the spec-file to not include such a beginner-weird way to define a function. (Or am I the only one/one of a few who had this problem? I don’t want to exclude the possibility, that I was the problem :smiley: )


You may have already learned this in researching further, but a function with an equal sign like the one you mentioned is known as a “setter” method while the non-equals sign method is a “getter” method. I’m also learning, but I believe it’s more common practice to use the shorthand attr_reader (for getters), attr_writer (for setters) or attr_accessor (for both) than to write out a bunch of methods with “=”. Initialize will only be called upon creating a new object, while the setter (and attr_writer) method lets you update data after its creation.

I believe it was discussed further in the Object-Oriented Programming Assignment #4: read “Ruby Explained: Classes” -

I like this article for a quick run-down of these types of methods:

Hope this helps!


Clear as day, thank you!


I had to literally search up the error I was getting and felt like I was cheating.
But, I found this really simple explanation on Stack Overflow that cleared it up for me. :}

I have a list of things I want to contribute to after I’m done, so I added that to the list, because like you, I don’t recall EVER reading about this in any tutorial. :thinking: