A Tour of Ruby
This section is a guided, but meandering, tour through some of the most interesting features of Ruby. Everything discussed here will be documented in detail later in the book, but this first look will give you the flavor of the language.
Ruby Is Object-Oriented
We’ll begin with the fact that Ruby is a
completely object-oriented language. Every value
is an object, even simple numeric literals and the values true, false, and nil (nil is a special value that indicates the
absence of value; it is Ruby’s version of null). Here we invoke a method named class on these values. Comments begin
with # in Ruby, and
the => arrows in the comments indicate the value returned by the
commented code (this is a convention used throughout this
book):
1.class # => Fixnum: the number 1 is a Fixnum 0.0.class # => Float: floating-point numbers have class Float true.class # => TrueClass: true is a the singleton instance of TrueClass false.class # => FalseClass nil.class # => NilClass
In many languages, function and method invocations require parentheses, but there are no parentheses in any of the code above. In Ruby, parentheses are usually optional and they are commonly omitted, especially when the method being invoked takes no arguments. The fact that the parentheses are omitted in the method invocations here makes them look like references to named fields or named variables of the object. This is intentional, but the fact is, Ruby is very strict about encapsulation of its objects; there is ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access