— Matz, the creator of Ruby
puts 'Hello, world!'
2 + 2 #=> 4
2 + 3.14 #=> 5.14
2 ** 100 #=> 1267650600228229401496703205376
2.class #=> Fixnum
3.14.class #=> Float
(2**100).class #=> Bignum
x = 1 # x is an integer (class FixNum)
x = x + 1.5 # x is now a Float
x = 'Ruby' # Now a string
puts "Hello, #{x}!" #=> 'Hello, Ruby!'
'Hello, world!'
'Hello'.class #=> String
'Hello'.size #=> 5
items = [1, 2, 3]
items << 'a' #=> [1, 2, 3, 'a']
x = items.pop #=> 'a'
items #=> [1, 2, 3]
items.push 4 #=> [1, 2, 3, 4]
(1..3)
(1..3).class #=> Range
(1..3).to_a #=> [1, 2, 3]
person = {
name: 'Matz',
languages: ['C', 'Python', 'Ruby']
}
# OR
person = { 'Matz' }
person[:languages] = ['C', 'Python', 'Ruby']
(0..2).each do |i|
puts i*2
end
if x == 2
do_something()
end
do_something if x == 2
do_something if x == true
do_something if x
def sqare(x)
x*x
end
square(3) #=> 9
Array of squares
list = (1..3)
squares = list.map { |i| i * 2 } #=> [1, 4, 9]
Using our square() method
list = (1..3)
squares = list.map :square #=> [1, 4, 9]
Sum of the numbers
list = (1..3)
squares = list.reduce :+
Select/reject items
(1..5).select { |i| i%2 == 0 } #=> [2, 4]
(1..5).reject { |i| i%2 == 0 } #=> [1, 3, 5]
Or even shorter
(1..5).select :even? #=> [2, 4]
(1..5).reject :even? #=> [1, 3, 5]
def foo
puts 'Before yield'
yield
puts 'After yield'
end
foo { puts 'Inside the block' }
#=> Before yield
#=> Inside the block
#=> After yield
foo = Proc.new do
puts 'Hello'
end
proc.call
hello = lambda { |x| puts "Hello, #{x}!" }
hello.call 'world' #=> Hello, world!
class Dog
def greet
'Woof!'
end
end
dog = Dog.new
dog.greet #=> 'Woof!'
class Dog
def name=(n)
@name = n
end
def greet
"Woof, I'm #{@name}"
end
end
dog = Dog.new
dog.name = 'Scooby'
dog.greet #=> 'Woof! I'm Scooby.'
class Dog
def initialize(name)
@name = name
end
def greet
"Woof, I'm #{@name}"
end
end
dog = Dog.new 'Scooby'
dog.greet #=> 'Woof! I'm Scooby.'
class Cat
@@sound = 'Meow'
def greet
@sound
end
end
garfield = Cat.new
garfield.greet #=> 'Meow!'
class Dog
def name=(name)
@name = name
end
def name
@name
end
end
class Dog
attr_accessor :name
end
# Also
# * attr_reader
# * attr_writer
class Fixnum
def plus_five
self + 5
end
end
1.plus_five #=> 6
class Animal
def initialize(name)
@name = name
end
def greet
'Hello!'
end
end
class Dog < Animal
def greet
"Hello, I'm #{@name}! Woof, woof!"
end
end
goofy = Dog.new('Goofy')
goofy.greet #=> "Hello, I'm Goofy! Woof, woof!"
class Cat < Animal
# do nothing here
end
tom = Dog.new('Tom')
tom.greet #=> "Hello!"
class Dog
def self.default_greeting
'Woof!'
end
end
Dog.default_greeting #=> 'Woof!'
class Dog
def greet
'Woof'
end
end
scooby = Dog.new
scooby.greet #=> 'Woof'
# ... contd
def scooby.greet
'Scooby Doo!'
end
scooby.greet #=> 'Scooby Doo!'
module Speech
def greeting
'Hello'
end
end
class Dog
extend Speech
end
Dog.greeting #=> 'Hello'
module Speech
def greet
'Hello'
end
end
class Dog
include Speech
end
dog = Dog.new
dog.greet #=> 'Hello'
class Dog
def method_missing(method_name, *args)
"Ï don't understand!"
end
end
scooby = Dog.new
scooby.hello #=> "I don't understand!"
class Dog
def method_missing(method_name, *args)
if method_name.to_s =~ /^say_(.*)/
"Dog says: #{$1}"
end
end
end
scooby = Dog.new
scooby.hello #=> nil
scooby.say_hello #=> 'Dog says: hello'
Run Ruby code in the browser
Interactive Ruby tutorials in the browser