Skip to content

Special Characters In Ruby Method Names

September 6, 2012

Yesterday I watched a short video on Code School’s Website regarding special character methods in Ruby.  It was a very interesting screencast.  What I learned that was most valuable to me is that special characters in method names are there only for the purpose of being expressive.

Take this sample class as an example from the video:

In this sample code, for the Banana class, the “tasty?” method has a question mark as a special character.  However, the question mark really has no value when it comes to syntax.  The “tasty?” method will execute the same way even if the question mark is absent from the method name.  However, by convention, the special character is written to be more expressive when reading the code.

This actually helped me quite a bit because I kept wondering why special characters exist in some method names and not others.  Now, I have the answer.

Finally, just in case this code doesn’t make any sense to you, here is an explanation of what it’s doing:

This is a Ruby class and the initialize method creates (or constructs) an instance of a banana.  The “tasty?” method is just a boolean that says if a banana object is black, then it’s not tasty, else it is tasty.  What’s interesting to note here is that you could instantiate this class multiple times, and each instance could be either tasty or not tasty depending on each individual object’s color.

If this doesn’t make sense right now, don’t worry.  I’ll cover classes in more detail later as I work my way through my learning material.  Until next time!

Advertisements

From → Ruby

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: