Ruby Sigils

Here’s a quick summary of the various sigils you’ll find while coding ruby. I acually had no idea that non-alphanumeric characters decorating variables were known as sigils in computer science, but sure enough they are! Heh, you learn something every day. Anyway, on to the sigils;

Variable Sigils

That is, sigils that accompany variables. These will always go before a variable, never after. Ruby is generally more forgiving than other languages in its naming conventions for things like methods and variables, but if you try things like;

a$, b@ or c!

you will receive a compiler error. Variable sigils come in the following flavours;

Sigil Example Usage Rubyists call them… You may know them as…
no sigil x, i, j, count Local Variables Scope Variables, Temporary Variables
@ @name, @age Object Variables Instance Variables, Member Variables, Members
$ $settings Global Variables probably also Global Variables
@@ @@last_invoked Class Variables Static Variables (although, not strictly speaking the same thing)
* *args Listed Arguments Probably the same thing. Put this in a method definition’s parameters and it’ll put them in an array for you
& &block Code Block parameter As above, but for code blocks/td>

Method Sigils

That is, sigils that exist within method names. It should be pointed out that these are not, strictly speaking, sigils. Ruby is insanely forgiving in letting you put most symbols straight into the method name. It will probably shock anyone non-familiar with ruby (but familiar with other languages) to find out that doing this;

> 2 + 3
=> 5

is just a little syntactic sugar coating the fact that “+” is a method on the number 2 (itself an instance of the “Fixnum” class!). The following is also perfectly valid, and is in fact exactly the same as the above;

> 2.+(3)
=> 5

Out of the ability to do this there follow two conventions when naming methods. Ruby conventions generally are out of the scope of this article (but boy, do they exist!) but while we are talking about weird symbols I thought they were worth a mention. Note that these are conventions, not features of the language;

Symbol Example Usage Meaning…
? even? prime? nil? Indicates that this method will return a boolean value
! sort! slice! collect! Indicates that this method will actually alter the object that it is being performed upon. For example, assuming arr is an array, arr.sort will return a sorted array but leave arr unchanged, whereas arr.sort! will also return a sorted array but will alter arr as well.

One thought on “Ruby Sigils

  1. Hey Mikey, I’m new to Ruby and Ruby on Rails. I’m a Java and PHP developer and have been looking for this kind of information online! Thanks for this! It helps a lot for knowing what I’m looking at now!

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s