Upload
eddie-kao
View
1.600
Download
7
Tags:
Embed Size (px)
Citation preview
Let’s Learn Ruby - Basic
Ruby Tuesday
https://www.facebook.com/groups/142197385837507/photo by othree
WebConf Taiwan 2014
750+ attendees
all tickets sold out in 4 mins
Let’s Learn Ruby
What I want?
Let’s Learn Ruby
Problem Solving
Let’s Learn Ruby
Active Ecosystem
Let’s Learn Ruby
Scenario
Let’s Learn Ruby
open source projects on Github
Let’s Learn Ruby
History
Let’s Learn Ruby
まつもと ゆきひろ (Matz)
Let’s Learn Ruby
Let’s Learn Ruby
first released at 1995
Let’s Learn Ruby
2.0 released at 2013
Let’s Learn Ruby
2.1 released at 2013.12
Let’s Learn Ruby
Why Ruby? free, open source, easy to learn
Let’s Learn Ruby
Ruby != Rails
Let’s Learn Ruby
Happy, and Fun
Let’s Learn Ruby
Rubies CRuby(MRI), REE, mRuby, MacRuby,
JRuby, IronRuby, Rubinius..etc
Let’s Learn Ruby
Version 1.8, 1.9, 2.0, 2.1
Let’s Learn Ruby
Ruby 1.8 has no future
Let’s Learn Ruby
Editors Vim, Emacs, Sublime Text... etc
Let’s Learn Ruby
But Ruby is Slow..?
Let’s Learn Ruby
What can Ruby do?
Let’s Learn Ruby
Rake Make, but Ruby version.
Rack http://rake.rubyforge.org/
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.comRuby on Rails http://rubyonrails.org/
Let’s Learn Ruby
developing MacOS and iOS app
Let’s Learn Ruby
drawing, image processing, music..
Let’s Learn Ruby
Install Ruby now!
Let’s Learn Ruby
Interactive Ruby, irb
Let’s Learn Ruby
Gem
Let’s Learn Ruby
gem install PACKAGE_NAME
Let’s Learn Ruby
gem env
Let’s Learn Ruby
gem list
Let’s Learn Ruby
Variables and Constants
Let’s Learn Ruby
local variable variable
Let’s Learn Ruby
global variable $variable
Let’s Learn Ruby
instance variable @variable
Let’s Learn Ruby
class variable @@variable
Let’s Learn Ruby
virtual variable true, false, self, nil
Let’s Learn Ruby
variable assignment a = 1
x, y, z = 1, 2, 3
Let’s Learn Ruby
Constant begins with a capital letter,
and it can be changed
Let’s Learn Ruby
Reserved word and Keyword
Let’s Learn Ruby
Reserved word and Keyword
Let’s Learn Ruby
Logic and Flow Control
Let’s Learn Ruby
only false and nil are false
Let’s Learn Ruby
true v.s TrueClass false v.s FalseClass
nil v.s NilClass
Let’s Learn Ruby
if..elsif..end
Let’s Learn Ruby
unless = not if
Let’s Learn Ruby
if modifier
Let’s Learn Ruby
case .. when..
Let’s Learn Ruby
BEGIN{} and END{}
Let’s Learn Ruby
a = true ? 'a' : 'b'
Let’s Learn Ruby
a ||= 'a'
Let’s Learn Ruby
Comment # single line
Let’s Learn Ruby
Comment =begin .. =end
Let’s Learn Ruby
Loop and Iteration
Let’s Learn Ruby
for.. in..
Let’s Learn Ruby
while .. end
Let’s Learn Ruby
until .. end
Let’s Learn Ruby
until = not while
Let’s Learn Ruby
times
Let’s Learn Ruby
upto, downto
Let’s Learn Ruby
each, each_with_index
Let’s Learn Ruby
Integer
http://www.ruby-doc.org/core-2.1.0/Integer.html
Let’s Learn Ruby
Fixnum and Bignum
Let’s Learn Ruby
10 / 3
Let’s Learn Ruby
String
http://ruby-doc.org/core-2.1.0/String.html
Let’s Learn Ruby
single and double quotes
Let’s Learn Ruby
%q v.s %Q
Let’s Learn Ruby
"%s" % "eddie"
Let’s Learn Ruby
string interpolation
Let’s Learn Ruby
Exercise please calculate how many “characters” and “words” of a section of a random article with Ruby.
Let’s Learn Ruby
Exercise please convert string “abcdefg” to “gfedcba” without using String#reverse method.
Let’s Learn Ruby
Array
http://ruby-doc.org/core-2.1.0/Array.html
Let’s Learn Ruby
Array.new v.s []
Let’s Learn Ruby
%w
Let’s Learn Ruby
Exercise please sort a given array [1, 3, 4, 1, 7, nil, 7], and remove nil and duplicate number.
Let’s Learn Ruby
Exercise please covert a given array [1, 2, 3, 4, 5] to [1, 3, 5, 7, 9] with Array#map method.
Let’s Learn Ruby
Exercise please draw 5 unique random number between 1 to 52.
Let’s Learn Ruby
Hash
http://ruby-doc.org/core-2.1.0/Hash.html
Let’s Learn Ruby
Hash.new v.s {}
Let’s Learn Ruby
a = { :name => 'eddie' } a = { name: 'eddie' }
Let’s Learn Ruby
Range
http://ruby-doc.org/core-2.1.0/Range.html
Let’s Learn Ruby
(1..10) v.s (1...10)
Let’s Learn Ruby
Exercise please calculate the sum from 1 to 100 with Range.
Let’s Learn Ruby
Methods
Let’s Learn Ruby
def method_name(param) ... end
Let’s Learn Ruby
parentheses can be omitted
Let’s Learn Ruby
? and !
Let’s Learn Ruby
return value
Let’s Learn Ruby
Singleton Method
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
Let’s Learn Ruby
Method Missing
Let’s Learn Ruby
def method_missing(method_name) puts "method: #{method_name} is called!" end !
something_not_exists()
Let’s Learn Ruby
Exception Handling begin .. rescue.. else.. ensure.. end
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
Let’s Learn Ruby
Block
Let’s Learn Ruby
Proc
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
Let’s Learn Ruby
lambda, ->
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
Proc v.s lambda
Let’s Learn Ruby
def proc_test puts "hello" my_proc = Proc.new { return 1 } my_proc.call puts "ruby" end def lambda_test puts "hello" my_lambda = lambda { return 1 } my_lambda.call puts "ruby" end
Let’s Learn Ruby
{} v.s do..end
http://blog.eddie.com.tw/2011/06/03/do-end-vs-braces/
Let’s Learn Ruby
Yield
Let’s Learn Ruby
Object-Oriented Programming
Let’s Learn Ruby
everything in Ruby is an Object
Let’s Learn Ruby
object = state+ behavior
Let’s Learn Ruby
root class = Object root class would be BasicObject in Ruby 1.9
Let’s Learn Ruby
class ClassName < ParentClass ... end
Let’s Learn Ruby
Naming Convention
Let’s Learn Ruby
initialize
Let’s Learn Ruby
ClassName.new
Let’s Learn Ruby
self = current object
Let’s Learn Ruby
instance and class variable
Let’s Learn Ruby
instance and class method
Let’s Learn Ruby
Exercise please create a Dog class and Cat class, which are both inherited from Animal class, and implement “walk” and “eat” methods.
Let’s Learn Ruby
public, protected and private method
Let’s Learn Ruby
getter and setter
Let’s Learn Ruby
attr_reader, attr_writer and attr_accessor
Let’s Learn Ruby
Open Class
Let’s Learn Ruby
Module
Let’s Learn Ruby
module ModuleName ... end
Let’s Learn Ruby
module has no inheritance
Let’s Learn Ruby
module has no instance
Let’s Learn Ruby
Naming Convention
Let’s Learn Ruby
require v.s load
Let’s Learn Ruby
Priority?
Let’s Learn Ruby
Exercise please create a Bird class, which is also inherited from Animal class, but include a Fly module.
Let’s Learn Ruby
Mixin
Let’s Learn Ruby
Ruby is single inheritance
Let’s Learn Ruby
Duck Typing
Let’s Learn Ruby
include v.s extend
Let’s Learn Ruby
Bundle
Let’s Learn Ruby
Gemfile
Let’s Learn Ruby
gem "nokogiri", :git => "git://github.com/tenderlove/nokogiri.git" gem "secret_gem", :path => "~/my_secret_path"
Let’s Learn Ruby
bundle install
Let’s Learn Ruby
pack your own gem!
Let’s Learn Ruby
1. bundle gem NEW_NAME 2. gem build NEW_NAME.gemspec 3. gem push NEW_NAME.gem
http://guides.rubygems.org/make-your-own-gem/
Let’s Learn Ruby
Exercise please try to create a Gem spec with bundle command, modify, build and push to rubygems.org.
Let’s Learn Ruby
Rake
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
Let’s Learn Ruby
task :goto_toliet do puts "goto toliet" end ! task :open_the_door => :goto_toliet do puts "open door" end
Let’s Learn Ruby
TDD
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
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
Ruby Object Model
Let’s Learn Ruby
At last..
photo by redjar
Let’s Learn Ruby
pick up one scripting language
photo by Quality & Style
Let’s Learn Ruby
Ruby is fun!
Let’s Learn Ruby
The only limitation is your imagination.
⾼高⾒見⻯⿓龍Contacts
photo by Eddie
Website
Blog
Plurk
Google Plus
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
+886-928-617-687