Section 1.7. Conclusion


1.6. Ruby Jargon and Slang

You 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 term attribute is used somewhat unofficially. We can think of an attribute as being an instance variable that is exposed to the outside world via one of the attr family of methods. This is a gray area because we could have methods such as foo and foo= that don't correspond to @foo as we would expect. And certainly not all instance variables are considered attributes. As always, common sense should guide your usage.

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 name of the attr_accessor method but disagrees with common usage in other communities where an accessor is read-only.

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 trendy" name for this is the threequal operator ("three equals").

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 indicate the omission of needless punctuation and tokens (a tongue-in-cheek reference to the punctuation habits of poets in the last six decades or so). Poetry mode is often taken to mean "omission of parentheses around method calls":

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 confused easily. When method calls are nested, it is better to use parentheses for clarity. In some cases, warnings are printed in current versions of Ruby:

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 open to discussion; I would say that it refers to the tendency of Ruby to be less concerned with the class of an object and more concerned with what methods can be called on it and what operations can be performed on it. Thus in Ruby we rarely use is_a? or kind_of, but we might more often use respond_to?. Most often of all, we might simply pass an object to a method and know that an exception will be raised if it is used inappropriately. This usually happens sooner rather than later.

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 heard that. Terms such as star and splat are inevitable in hacker circles along with derivatives such as splatted and unsplatted. David Alan Black cleverly calls this the unary unarray operator.

The term singleton is sometimes regarded as overused. It is useful to remember that this is a perfectly good English word in its own rightreferring to a thing that is solitary or unique. As long as we use it as a modifier, there should be no confusion.

The Singleton Pattern is a well-known design pattern in which a class allows itself to be instantiated only once; the singleton library in Ruby facilitates the use of this pattern.

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 eigenvalue. This is clever, but this usage is not communitywide, and some people hate it.

Let's go back to our previous example. Because the method hyphenate exists in no other object or class, it is a singleton method on that object. This also is unambiguous. Sometimes the object itself will be called a singleton because it is one of a kindit is the only object that has that 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 slight misnomer because of its nontrivial behavior with regard to inheritance. A class instance variable is a somewhat different animal. It is an ordinary instance variable where the object it belongs to happens to be a class. For more information, see Chapter 11, "OOP and Dynamic Features in Ruby."




The Ruby Way(c) Solutions and Techniques in Ruby Programming
The Ruby Way, Second Edition: Solutions and Techniques in Ruby Programming (2nd Edition)
ISBN: 0672328844
EAN: 2147483647
Year: 2004
Pages: 269
Authors: Hal Fulton

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net