intro to ruby
TRANSCRIPT
Ruby
The Ruby Language
• Originally by Yukihiro "Matz" Matsumoto• “Ruby is designed for programmer
productivity and fun, following the principles of good user interface design. He stresses that systems design needs to emphasize human, rather than computer, needs.”
http://en.wikipedia.org/wiki/Ruby_(programming_language)#History
• Ruby 1.0 was released in 1996.
Ruby Language Overview
• Dynamically typed• Interpreted• Can be modified at runtime• Object oriented• Blocks & lambdas• Nice support for Regular Expressions
Lets get started
• IRB: InteractiveRuBy>> 4>> 4 + 4
Everything is an object
“test”.upcase“test”.class“test”.methods
Everything evaluates to something
2 + 2(2+2).zero?
Methods are Messages
thing.do(4)thing.do 4thing.send “do”, 4
Operators are Methods
thing.do 4thing.do(4)thing.send “do”, 4
1 + 21.+(2)1.send "+", 2
Write your first Ruby class
Conditionals, Iterators & Blocks
With Hashes & Arrays
CONDITIONALS
Conditionals: ifif age > 17puts “can vote”
end
if age > 17puts “can vote”
elseputs “attends school”
end
Statement Modifiers:y = 7 if x == 4
Other Syntax:if x == 4 then y = 7
end
Truth• Truth: Everything is true except for:– false – nil
• Therefore– 0 is true– “” is true
• Checking for false:if !(name == “superman”) …if not (name == “superman”) …
Unless
• “unless” provides us with another way of checking if a condition is false:
unless superpower == nilstatus = “superhero”
end
Case
case superherowhen “superman”
city = “metropolis”when “batman”
city = “gotham_city”else
city = “central_city”end
Case Refactoring
city = case superherowhen “superman”
“metropolis”when “batman”
“gotham_city”else
“central_city”end
ITERATORS
Iterators: Conditional Looping
• “while” allows us to loop through code while a set condition is true
x = 1while x < 10puts x.to_s + “ iteration”x += 1
end
Creating a new array
x = [1, 2, 3, 4]=> [1, 2, 3, 4]
x = %w(1 2 3 4) => [“1”, “2”, “3”, “4”]
chef = Array.new(3, “bork”)=> [“bork”, “bork”, bork”]
Accessing Array Values
a = [ "a", "b", "c", "d", "e" ] a[0] #=> "a”
a[2] #=> "c”
a[6] #=> nil
Creating a Hash
h = { "a" => 100, "b" => 200 }h[“a”]
h = { 1 => “a”, “b” => “hello” }h[1]
Operations on Hashes: Merge
h1 = { "a" => 100, "b" => 200 } => {"a"=>100, "b"=>200}h2 = { "b" => 254, "c" => 300 }=>{"b"=>254, "c"=>300}
h3 = h1.merge(h2)=> {"a"=>100, "b"=>254, "c"=>300}h1=> {"a"=>100, "b"=>200}
h1.merge!(h2)=> {"a"=>100, "b"=>254, "c"=>300}
Operations on Hashes
h = { "a" => 100, "b" => 200 } h.delete("a”)
h = { "a" => 100, "b" => 200, "c" => 300, "d" => 400 }
letters = h.keys
h = { "a" => 100, "b" => 200, "c" => 300 }
numbers = h.values
Times
5.times{ puts “hello” }
99.times do |beer_num| puts "#{beer_num} bottles
of beer”end
Eachsuperheroes = [“catwoman”, “batman”, “wonderwoman”]superheroes.each do | s |
puts “#{ s } save me!” end
wonderwoman save me!batman save me!catwoman save me!
BLOCKS
Blocks
def dos_veces yield yieldend
dos_veces { puts "Hola” }
HolaHola
This is a Block!
{Yield executes the block
Yield with Parameters
def bandsyield(“abba”, “who”)
end
bands do |x,y| puts x,y
end
abbawho
Yield sends its parameters as arguments to the block
yield(“abba”, ”who”) sends “abba” and “who” to |x, y|
x is set to “abba”
y is set to “who”
Ruby Koansa walk along the path to enlightenment
http://github.com/edgecase/ruby_koans
Mr. Neighborly's Humble Little
Ruby BookJeremy McAnally
The Well-Grounded RubyistCovering Ruby 1.9
David A. Black