Despite being an object-oriented language, it is not necessary to use explicitly object-oriented syntax within a basic Ruby program. While everything works on objects (and methods called upon those objects) behind the scenes, you can write a program as simply as this:
def fib(i)
if i.zero?
0
elsif i == 1
1
else
fib(i - 2) + fib(i - 1)
end
end
puts fib(10)
This script prints to screen the 10th number in the Fibonacci sequence. It defines a method called fib that returns the relevant result from a simple if/elsif/else expression. Note the use of standard equality syntax (==), addition (+), subtraction (-), and method calling (fib(10)), but also note the possibility of using methods in somewhat idiomatic ways (i.zero? rather than i == 0—though the latter would also work). The use of i.zero? demonstrates calling a method upon an object (where i is the object, and zero? is the method).
The main Ruby interpreter is usually invoked by running "ruby" from the command line. If it is given a filename as an argument that file will be run (e.g. ruby myscript.rb). The interpreter has several other options that are listed in the "Ruby Interpreter Arguments" table in this card's reference section.
Developing a program with "true" object-oriented syntax is not significantly different. For example:
class Person
attr_accessor :name, :age
def full_info
return "#{@name} is #{@age} years old"
end
end
fred = Person.new
fred.name = "Fred"
fred.age = 45
puts fred.full_info
In this example, a class (Person) is defined, and attributes (name and age) and a method (full_info) are defined upon that class. Below the class definition, we then create an instance of the Person class and assign it to a variable, fred, before assigning values to its attributes, and then calling that instance's full_info method (which, in turn, uses instance variables—prefixed with @-to create its output).
"This is a test" is a string with no special qualities (and, remember, it's also an object in Ruby) but it's possible to interpolate data into it (from variables, etc.) with a special syntax:
"2 plus 2 is #{2 + 2}"
The #{} construction serves to interpolate the result of the expression within the curly braces—in this case 2 + 2 is calculated to equal 4 and so the string ends up as "2 plus 2 is 4"
Earlier we called Ruby a "reflective" language because it offers functionality to programs to change, extend, and otherwise inspect themselves. We can look at a key Ruby idiom and reflective feature—class reopening—by changing the Fibonacci example from earlier to the following:
class Integer
def fib
if self.zero?
0
elsif self == 1
1
else
(self - 2).fib + (self - 1).fib
end
end
end
puts 10.fib
Note this time that in order to get the Fibonacci number, we're no longer calling a global fib method, but a method that works directly upon the number 10 itself (remember, everything is an object—even the number 10!). The way this is achieved is by "reopening" a standard Ruby class—Integer—and defining a new method called fib within it. This then makes the fib method available to all objects of class Integer in Ruby! Note that the content of the integer object itself (the number we need to use) is obtained with the self keyword. self, in this case, returns a representation of the current object in its native form. In this sense, Ruby is very similar to Python.
{{ parent.title || parent.header.title}}
{{ parent.tldr }}
{{ parent.linkDescription }}
{{ parent.urlSource.name }}