































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!
































A programming language
Developed by Yukihiro
Matsumoto (aka Matz) in the 1990s
Ruby is largely and loosely
based on perl (hence the name, according to lore)
Completely object oriented
Javascript--born of the competition between two companies PHP--created by a varied community Ruby--the vision of a single person Rails--the vision of another single person When you compare these, you can see how the starting point influences the process of development
Ruby is an interpreter, just like php or bash: Avatar:~ hays$ ruby print "howdy world!" ^d Or, use ruby -e "command": ruby -e 'puts "hello\n"' Or, you can just use irb, which is easier: Avatar:~ hays$ irb
print "howdy world!" howdy world!=> nil
A class is a kind of master
object
Can contain constants and
methods
Instances of object can be
created from a class, inheriting the traits of the class
class Cat end
(but this class doesn't do or mean anything)
the class examples are derived from http://www.juixe.com/techknow/index.php/2007/01/22/ruby-class-tutorial/
Now, I can create an instance
of the cat class: gc = Cat.new("GC", "short hair", "black", "gimpy") lc = Cat.new("LC", "short hair", "black", "little")
I'd like to be able to describe my cats easily So I add a method to the cat class: def describe @name + " is a " + @color + " "
If I call a cat with the
describe method attached, I can get the description of that cat: my_string= gc.describe puts my_string
or:
puts gc.describe
A second method, find_by_name: def self.find_by_name(name) found = nil ObjectSpace.each_object(Cat) { | o| found = o if o.name == name } found end
In ruby, vars are references to objects, not objects themselves So: a = "my value" b = a a[0] = "n" will change both a and b--but if you reassign a, eg a="new value", a is linked to a new object (this might bite you, but it's not likely)
Create an array by assignment:
my_array = [ "one", "two", 3, 4 ]
Referencing the array:
puts "my_array[0] is: #{my_array[0]}\n"
The brackets are methods of
the array class…