3. “I always thought Smalltalk would beat Java.I just didn’t
know if would be called ‘Ruby’ when it did so”
- Kent Beck -
seesaw
lunedì 4 marzo 13
4. The Ruby Language
• Designed for programmer productivity and fun
• Generic, interpreted, reflective, with garbage collection
• Optimized for people rather than computers
• More powerful than Perl, more object oriented than Python
• Everything is an object. There are no primitive types
• Strong dynamic typing
seesaw
lunedì 4 marzo 13
5. implementations
• Matz's Ruby Interpreter or MRI, uses its own Ruby-specific virtual machine
written in C
• JRuby, runs on the Java virtual machine
• Rubinius, C++ bytecode VM that uses LLVM to compile to machine code at
runtime
• MagLev, a Smalltalk implementation
• MacRuby, an OS X implementation on the Objective-C runtime
• IronRuby, an implementation on the .NET Framework
seesaw
lunedì 4 marzo 13
6. versions
v0.95 - December 21, 1995
v1.0 - December 25, 1996
v1.2 - December 1998
v1.3 - year1999
v1.4 - August 1999
v1.6 - September 2000
v1.8 - August 2003
Ruby on Rails - 2005
v1.9 - December 2007
seesaw
lunedì 4 marzo 13
7. Everything in Ruby is
‣ Assignment - binding names to objects
‣ Control structures - if/else, while, case
‣ Sending messages to objects - methods
seesaw
lunedì 4 marzo 13
9. strings
a = "nThis is a double-quoted stringn"
a = %Q{nThis is a double-quoted stringn}
a = %{nThis is a double-quoted stringn}
a = %/nThis is a double-quoted stringn/
a = <<-BLOCK
This is a double-quoted string
BLOCK
a = 'This is a single-quoted string'
a = %q{This is a single-quoted string}
seesaw
lunedì 4 marzo 13
11. associative arrays
hash = Hash.new
hash = { :water => 'wet', :fire => 'hot' }
puts hash[:fire]
# => hot
hash.each do |key, value|
puts "#{key} is #{value}"
end
# => water is wet
# fire is hot
hash.delete :water
# Deletes water: 'wet'
hash.delete_if { |key,value| value == 'hot'}
# Deletes :fire => 'hot'
seesaw
lunedì 4 marzo 13
12. blocks & iterators
do
puts "Hello, World!"
end
oppure
{ puts "Hello, World!" }
seesaw
lunedì 4 marzo 13
13. closures
# In an object instance variable (denoted with '@'),
remember a block.
def remember(&a_block)
@block = a_block
end
# Invoke the above method, giving it a block which takes a
name.
remember {|name| puts "Hello, #{name}!"}
# When the time is right (for the object) -- call the
closure!
@block.call("Jon")
# => "Hello, Jon!"
seesaw
lunedì 4 marzo 13
14. closures
def create_set_and_get(initial_value=0) # Note the default value of 0
closure_value = initial_value
return Proc.new {|x| closure_value = x}, Proc.new { closure_value }
end
setter, getter = create_set_and_get # ie. returns two values
setter.call(21)
getter.call # => 21
seesaw
lunedì 4 marzo 13
15. closures
def create_set_and_get(initial_value=0) # Note the default value of 0
closure_value = initial_value
return Proc.new {|x| closure_value = x}, Proc.new { closure_value }
end
setter, getter = create_set_and_get # ie. returns two values
setter.call(21)
getter.call # => 21
#You can also use a parameter variable as a binding for the closure.
#So the above can be rewritten as...
def create_set_and_get(closure_value=0)
return proc {|x| closure_value = x } , proc { closure_value }
en
seesaw
lunedì 4 marzo 13
16. yield
def use_hello
yield "hello"
end
# Invoke the above method, passing it a block.
use_hello {|string| puts string} # => 'hello'
seesaw
lunedì 4 marzo 13
17. enumeration
array = [1, 'hi', 3.14]
array.each {|item| puts item }
# => 1
# => 'hi'
# => 3.14
array.each_index do|index|
puts "#{index}: #{array[index]}"
end
# => 0: 1
# => 1: 'hi'
# => 2: 3.14
# The following uses a Range
(3..6).each {|num| puts num }
# => 3
# => 4
# => 5
# => 6
seesaw
lunedì 4 marzo 13
18. functional programming
[1,3,5].inject(10) {|sum, element| sum + element}
# => 19
File.open('file.txt', 'w') do |file|
# 'w' denotes "write mode"
file.puts 'Wrote some text.'
end # File is automatically closed here
File.readlines('file.txt').each do |line|
puts line
end
(1..10).collect {|x| x*x}
# => [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
(1..5).collect(&:to_f)
# => [1.0, 2.0, 3.0, 4.0, 5.0]
seesaw
lunedì 4 marzo 13
19. classes
class Person
attr_reader :name, :age
def initialize(name, age)
@name, @age = name, age
end
def <=>(person) # Comparison operator for sorting
age <=> person.age
end
def to_s
"#{name} (#{age})"
end
end
group = [
Person.new("Bob", 33),
Person.new("Chris", 16),
Person.new("Ash", 23)
]
puts group.sort.reverse
seesaw
lunedì 4 marzo 13
20. monkey patching
# re-open Ruby's Time class
class Time
def yesterday
self - 86400
end
end
today = Time.now
# => Thu Aug 14 16:51:50 +1200 2012
yesterday = today.yesterday
# => Wed Aug 13 16:51:50 +1200 2012
seesaw
lunedì 4 marzo 13
21. metaprogramming
COLORS = { black: "000",
red: "f00",
green: "0f0",
yellow: "ff0",
blue: "00f",
magenta: "f0f",
cyan: "0ff",
white: "fff" }
class String
COLORS.each do |color,code|
define_method "in_#{color}" do
"<span style="color: ##{code}">#{self}</span>"
end
end
end
seesaw
lunedì 4 marzo 13
22. 2.0
fully backward compatible with Ruby 1.9.3
seesaw
lunedì 4 marzo 13