Let’s Learn Ruby
Rake
Make, but Ruby version.
Rack http://rake.rubyforge.org/
Slide 32
Slide 32 text
Let’s Learn Ruby
Rack
it’s a specification (and implementation) of a minimal
abstract Ruby API that models HTTP.
such as Sinatra, Ruby on Rails
Rack http://rack.rubyforge.org/
Sinatra http://www.sinatrarb.com
Ruby on Rails http://rubyonrails.org/
Let’s Learn Ruby
a = { :name => 'eddie' }
a = { name: 'eddie' }
Slide 91
Slide 91 text
Let’s Learn Ruby
Range
http://ruby-doc.org/core-2.1.0/Range.html
Slide 92
Slide 92 text
Let’s Learn Ruby
(1..10) v.s (1...10)
Slide 93
Slide 93 text
Let’s Learn Ruby
Exercise
please calculate the sum from 1 to 100 with
Range.
Slide 94
Slide 94 text
Let’s Learn Ruby
Methods
Slide 95
Slide 95 text
Let’s Learn Ruby
def method_name(param)
...
end
Slide 96
Slide 96 text
Let’s Learn Ruby
parentheses can be omitted
Slide 97
Slide 97 text
Let’s Learn Ruby
? and !
Slide 98
Slide 98 text
Let’s Learn Ruby
return value
Slide 99
Slide 99 text
Let’s Learn Ruby
Singleton Method
Slide 100
Slide 100 text
Let’s Learn Ruby
class Cat
def walk
puts "I'm walking"
end
end
!
cat = Cat.new
def cat.fly
puts "I can fly"
end
cat.fly
Slide 101
Slide 101 text
Let’s Learn Ruby
Method Missing
Slide 102
Slide 102 text
Let’s Learn Ruby
def method_missing(method_name)
puts "method: #{method_name} is called!"
end
!
something_not_exists()
Slide 103
Slide 103 text
Let’s Learn Ruby
Exception Handling
begin .. rescue.. else.. ensure.. end
Slide 104
Slide 104 text
Let’s Learn Ruby
def open_my_file(file_name)
File.open file_name do |f|
puts f.read
end
end
begin
open_my_file("block_demo.r")
rescue => e
puts e
else
puts "it's working good!"
ensure
puts "this must be executed, no matter what"
end
Slide 105
Slide 105 text
Let’s Learn Ruby
Block
Slide 106
Slide 106 text
Let’s Learn Ruby
Proc
Slide 107
Slide 107 text
Let’s Learn Ruby
my_square = Proc.new { | x | x ** 2 }
!
# how to call a proc
puts my_square.call(10) # 100
puts my_square[10] # 100
puts my_square.(10) # 100
puts my_square === 10 # 100
Slide 108
Slide 108 text
Let’s Learn Ruby
lambda, ->
Slide 109
Slide 109 text
Let’s Learn Ruby
my_lambda = lambda { | x | x ** 2 }
!
# new style in 1.9
my_lambda = -> x { x ** 2 }
!
# how to call a lambda?
puts my_lambda.call(10) # 100
puts my_lambda[10] # 100
puts my_lambda.(10) # 100
puts my_lambda === 10 # 100
Let’s Learn Ruby
Exercise
please try to create a Gem spec with bundle
command, modify, build and push to
rubygems.org.
Slide 150
Slide 150 text
Let’s Learn Ruby
Rake
Slide 151
Slide 151 text
Let’s Learn Ruby
desc "mail sender"
task :sendmail do
puts "grap mailing list from database..."
sleep 3
puts "mail sending..."
sleep 3
puts "done!"
end
Slide 152
Slide 152 text
Let’s Learn Ruby
task :goto_toliet do
puts "goto toliet"
end
!
task :open_the_door => :goto_toliet do
puts "open door"
end
Slide 153
Slide 153 text
Let’s Learn Ruby
TDD
Slide 154
Slide 154 text
Let’s Learn Ruby
require “minitest/autorun"
!
class TestMyBMI < MiniTest::Unit::TestCase
def test_my_calc_bmi_is_ok
assert_equal calc_bmi(175, 80), 26.12
end
end
!
def calc_bmi(height, weight)
bmi = ( weight / (height/100.0) ** 2 ).round(2)
end
Slide 155
Slide 155 text
Let’s Learn Ruby
require "minitest/autorun"
describe "test my bmi calculator" do
it "should calc the correct bmi" do
calc_bmi(175, 80).must_equal 26.12
end
end
def calc_bmi(height, weight)
bmi = ( weight / (height/100.0) ** 2 ).round(2)
end
Let’s Learn Ruby
The only limitation is your
imagination.
Slide 165
Slide 165 text
⾼高⾒見⻯⿓龍
Contacts
photo by Eddie
Website
Blog
Plurk
Facebook
Google Plus
Twitter
Email
Mobile
http://www.eddie.com.tw
http://blog.eddie.com.tw
http://www.plurk.com/aquarianboy
http://www.facebook.com/eddiekao
http://www.eddie.com.tw/+
https://twitter.com/eddiekao
[email protected]
+886-928-617-687