1.6. Ruby Jargon and SlangYou don't have to re-learn English when you learn Ruby. But certain pieces of jargon and slang are commonly used in the community. Some of these may be used in slightly different ways from the rest of the computer science world. Most of these are discussed in this section.
In Ruby, the
Attributes in Ruby can be broken down into
readers
and
writers
(called
getters
and
setters
in some languagesterms we don't commonly use). An
accessor
is both a reader and a writer; this is consistent with the
The operator
===
is unique to Ruby (as far as I am aware). The common name for it is the
case equality operator
because it is used implicitly by case statements. But this is slightly misleading, as I said earlier, because it is not really "equality" at all. In this book, I often use the term
relationship operator
. I did not invent this term, but I can't find its origin, and it is not in common use today. The "hip and
The <=> operator is probably best called the comparison operator . Common slang is spaceship operator because it looks like a side view of a flying saucer in an old-fashioned video game or text-based computer game.
The term
poetry mode
is used by some to
some_method(1, 2, 3) # unneeded parentheses some_method 1, 2, 3 # "poetry mode" But I think the principle is more general than that. For example, when a hash is passed as the last or lone parameter, the braces may be omitted. At the end of a line, the semicolon may be omitted (and really always is). In most cases, the keyword then may be omitted, whether in if statements or case statements. Some coders even go so far as to omit parentheses in a method definition, though most do not:
def my_method(a, b, c) # Also legal: def my_method a, b, c # ... end
It is worth noting that in some cases, the complexity of the Ruby grammar causes the parser to be
def alpha(x) x*2 end def beta(y) y*3 end gamma = 5 delta = alpha beta gamma # 30 -- same as alpha(beta(gamma)) # Produces warning: # warning: parenthesize argument(s) for future version
The term
duck typing
, as far as I can tell, originated with Dave Thomas. It refers to the old saying that if something looks like a duck, walks like a duck, and quacks like a duck, it might as well be a duck. Exactly what this term means may be
The unary asterisk that is used to expand an array could be called an
array expansion
operator, but I don't think I have ever
The term
singleton
is sometimes regarded as
The
Singleton Pattern
is a well-known design pattern in which a class allows itself to be
A singleton class in Ruby is the classlike entity where methods are stored that are per-object rather than per-class. It is arguably not a "true class" because it cannot be instantiated. The following is an example of opening up the singleton class for a string object and adding a method:
str = "hello"
class << str # Alternatively:
def hyphenated # def str.hyphenated
self.split("").join("-")
end
end
str.hyphenated # "h-e-l-l-o"
Some have started to use the term
eigenclass
for this, derived from the German
eigen
("its own"), and corresponding to terms from math and physics such as
Let's go back to our previous example. Because the method
But remember that in Ruby, a class is itself an object. Thus we can add a method to the singleton class of a class , and that method will be unique to that object, which happens to be a class. Here is an example:
class MyClass
class << self # Alternatively: def self.hello
def hello # or: def MyClass.hello
puts "Hello from #{self}!"
end
end
end
So we don't have to instantiate MyClass to call this method.
MyClass.hello # Hello from MyClass! But you will notice that this is simply what we call a class method in Ruby. In other words, a class method is a singleton method on a class. We could also say it's a singleton method on an object that happens to be a class.
There are a couple more terms to cover. A
class variable
is one that starts with a double-
@
, of course; it is perhaps a
|