Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Let's Learn Ruby - Basic

高見龍
December 30, 2013

Let's Learn Ruby - Basic

高見龍

December 30, 2013
Tweet

More Decks by 高見龍

Other Decks in Programming

Transcript

  1. Let’s Learn Ruby - Basic

    View full-size slide

  2. Ruby Tuesday
    https://www.facebook.com/groups/142197385837507/
    photo by othree

    View full-size slide

  3. RubyConf Taiwan
    http://rubyconf.tw/
    photo by othree

    View full-size slide

  4. Rails Girls Taipei
    https://www.facebook.com/railsgirlstw

    View full-size slide

  5. WebConf Taiwan 2014

    View full-size slide

  6. 750+ attendees

    View full-size slide

  7. all tickets sold out in 4 mins

    View full-size slide

  8. Let’s Learn Ruby
    What I want?

    View full-size slide

  9. Let’s Learn Ruby
    Problem Solving

    View full-size slide

  10. Let’s Learn Ruby
    Active Ecosystem

    View full-size slide

  11. Let’s Learn Ruby
    Scenario

    View full-size slide

  12. Let’s Learn Ruby
    open source projects on Github

    View full-size slide

  13. Let’s Learn Ruby
    History

    View full-size slide

  14. Let’s Learn Ruby
    まつもと ゆきひろ (Matz)

    View full-size slide

  15. Let’s Learn Ruby

    View full-size slide

  16. Let’s Learn Ruby
    first released at 1995

    View full-size slide

  17. Let’s Learn Ruby
    2.0 released at 2013

    View full-size slide

  18. Let’s Learn Ruby
    2.1 released at 2013.12

    View full-size slide

  19. Let’s Learn Ruby
    Why Ruby?
    free, open source, easy to learn

    View full-size slide

  20. Let’s Learn Ruby
    Ruby != Rails

    View full-size slide

  21. Let’s Learn Ruby
    Happy, and Fun

    View full-size slide

  22. Let’s Learn Ruby
    Rubies
    CRuby(MRI), REE, mRuby, MacRuby,
    JRuby, IronRuby, Rubinius..etc

    View full-size slide

  23. Let’s Learn Ruby
    Version
    1.8, 1.9, 2.0, 2.1

    View full-size slide

  24. Let’s Learn Ruby
    Ruby 1.8 has no future

    View full-size slide

  25. Let’s Learn Ruby
    RVM
    Ruby Version Manager
    https://rvm.io/

    View full-size slide

  26. Let’s Learn Ruby
    Editors
    Vim, Emacs, Sublime Text... etc

    View full-size slide

  27. Let’s Learn Ruby
    coding style
    https://github.com/styleguide/ruby

    View full-size slide

  28. Let’s Learn Ruby
    But Ruby is Slow..?

    View full-size slide

  29. Let’s Learn Ruby
    What can Ruby do?

    View full-size slide

  30. Let’s Learn Ruby
    Rake
    Make, but Ruby version.
    Rack http://rake.rubyforge.org/

    View full-size slide

  31. 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/

    View full-size slide

  32. Let’s Learn Ruby
    developing MacOS and iOS app

    View full-size slide

  33. Let’s Learn Ruby
    drawing, image processing,
    music..

    View full-size slide

  34. Let’s Learn Ruby
    Install Ruby now!

    View full-size slide

  35. Let’s Learn Ruby
    http://tryruby.org

    View full-size slide

  36. Let’s Learn Ruby
    Interactive Ruby, irb

    View full-size slide

  37. Let’s Learn Ruby
    Gem

    View full-size slide

  38. Let’s Learn Ruby
    gem install PACKAGE_NAME

    View full-size slide

  39. Let’s Learn Ruby
    gem env

    View full-size slide

  40. Let’s Learn Ruby
    gem list

    View full-size slide

  41. Let’s Learn Ruby
    Variables and Constants

    View full-size slide

  42. Let’s Learn Ruby
    local variable
    variable

    View full-size slide

  43. Let’s Learn Ruby
    global variable
    $variable

    View full-size slide

  44. Let’s Learn Ruby
    instance variable
    @variable

    View full-size slide

  45. Let’s Learn Ruby
    class variable
    @@variable

    View full-size slide

  46. Let’s Learn Ruby
    virtual variable
    true, false, self, nil

    View full-size slide

  47. Let’s Learn Ruby
    variable assignment
    a = 1
    x, y, z = 1, 2, 3

    View full-size slide

  48. Let’s Learn Ruby
    Constant
    begins with a capital letter,
    and it can be changed

    View full-size slide

  49. Let’s Learn Ruby
    Reserved word and Keyword

    View full-size slide

  50. Let’s Learn Ruby
    Reserved word and
    Keyword

    View full-size slide

  51. Let’s Learn Ruby
    Logic and Flow Control

    View full-size slide

  52. Let’s Learn Ruby
    only false and nil are false

    View full-size slide

  53. Let’s Learn Ruby
    true v.s TrueClass
    false v.s FalseClass
    nil v.s NilClass

    View full-size slide

  54. Let’s Learn Ruby
    if..elsif..end

    View full-size slide

  55. Let’s Learn Ruby
    unless = not if

    View full-size slide

  56. Let’s Learn Ruby
    if modifier

    View full-size slide

  57. Let’s Learn Ruby
    case .. when..

    View full-size slide

  58. Let’s Learn Ruby
    BEGIN{} and END{}

    View full-size slide

  59. Let’s Learn Ruby
    a = true ? 'a' : 'b'

    View full-size slide

  60. Let’s Learn Ruby
    a ||= 'a'

    View full-size slide

  61. Let’s Learn Ruby
    Comment
    # single line

    View full-size slide

  62. Let’s Learn Ruby
    Comment
    =begin .. =end

    View full-size slide

  63. Let’s Learn Ruby
    Loop and Iteration

    View full-size slide

  64. Let’s Learn Ruby
    for.. in..

    View full-size slide

  65. Let’s Learn Ruby
    while .. end

    View full-size slide

  66. Let’s Learn Ruby
    until .. end

    View full-size slide

  67. Let’s Learn Ruby
    until = not while

    View full-size slide

  68. Let’s Learn Ruby
    times

    View full-size slide

  69. Let’s Learn Ruby
    upto, downto

    View full-size slide

  70. Let’s Learn Ruby
    each, each_with_index

    View full-size slide

  71. Let’s Learn Ruby
    Integer
    http://www.ruby-doc.org/core-2.1.0/Integer.html

    View full-size slide

  72. Let’s Learn Ruby
    Fixnum and Bignum

    View full-size slide

  73. Let’s Learn Ruby
    10 / 3

    View full-size slide

  74. Let’s Learn Ruby
    String
    http://ruby-doc.org/core-2.1.0/String.html

    View full-size slide

  75. Let’s Learn Ruby
    single and double quotes

    View full-size slide

  76. Let’s Learn Ruby
    %q v.s %Q

    View full-size slide

  77. Let’s Learn Ruby
    "%s" % "eddie"

    View full-size slide

  78. Let’s Learn Ruby
    string interpolation

    View full-size slide

  79. Let’s Learn Ruby
    Exercise
    please calculate how many “characters” and
    “words” of a section of a random article with Ruby.

    View full-size slide

  80. Let’s Learn Ruby
    Exercise
    please convert string “abcdefg” to “gfedcba”
    without using String#reverse method.

    View full-size slide

  81. Let’s Learn Ruby
    Array
    http://ruby-doc.org/core-2.1.0/Array.html

    View full-size slide

  82. Let’s Learn Ruby
    Array.new v.s []

    View full-size slide

  83. Let’s Learn Ruby
    %w

    View full-size slide

  84. Let’s Learn Ruby
    Exercise
    please sort a given array [1, 3, 4, 1, 7, nil, 7],
    and remove nil and duplicate number.

    View full-size slide

  85. 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.

    View full-size slide

  86. Let’s Learn Ruby
    Exercise
    please draw 5 unique random number
    between 1 to 52.

    View full-size slide

  87. Let’s Learn Ruby
    Hash
    http://ruby-doc.org/core-2.1.0/Hash.html

    View full-size slide

  88. Let’s Learn Ruby
    Hash.new v.s {}

    View full-size slide

  89. Let’s Learn Ruby
    a = { :name => 'eddie' }
    a = { name: 'eddie' }

    View full-size slide

  90. Let’s Learn Ruby
    Range
    http://ruby-doc.org/core-2.1.0/Range.html

    View full-size slide

  91. Let’s Learn Ruby
    (1..10) v.s (1...10)

    View full-size slide

  92. Let’s Learn Ruby
    Exercise
    please calculate the sum from 1 to 100 with
    Range.

    View full-size slide

  93. Let’s Learn Ruby
    Methods

    View full-size slide

  94. Let’s Learn Ruby
    def method_name(param)
    ...
    end

    View full-size slide

  95. Let’s Learn Ruby
    parentheses can be omitted

    View full-size slide

  96. Let’s Learn Ruby
    ? and !

    View full-size slide

  97. Let’s Learn Ruby
    return value

    View full-size slide

  98. Let’s Learn Ruby
    Singleton Method

    View full-size slide

  99. 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

    View full-size slide

  100. Let’s Learn Ruby
    Method Missing

    View full-size slide

  101. Let’s Learn Ruby
    def method_missing(method_name)
    puts "method: #{method_name} is called!"
    end
    !
    something_not_exists()

    View full-size slide

  102. Let’s Learn Ruby
    Exception Handling
    begin .. rescue.. else.. ensure.. end

    View full-size slide

  103. 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

    View full-size slide

  104. Let’s Learn Ruby
    Block

    View full-size slide

  105. Let’s Learn Ruby
    Proc

    View full-size slide

  106. 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

    View full-size slide

  107. Let’s Learn Ruby
    lambda, ->

    View full-size slide

  108. 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

    View full-size slide

  109. Let’s Learn Ruby
    Proc v.s lambda

    View full-size slide

  110. 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

    View full-size slide

  111. Let’s Learn Ruby
    {} v.s do..end
    http://blog.eddie.com.tw/2011/06/03/do-end-vs-braces/

    View full-size slide

  112. Let’s Learn Ruby
    Yield

    View full-size slide

  113. Let’s Learn Ruby
    Object-Oriented
    Programming

    View full-size slide

  114. Let’s Learn Ruby
    everything in Ruby is an Object

    View full-size slide

  115. Let’s Learn Ruby
    object = state+ behavior

    View full-size slide

  116. Let’s Learn Ruby
    root class = Object
    root class would be BasicObject in Ruby 1.9

    View full-size slide

  117. Let’s Learn Ruby
    class ClassName < ParentClass
    ...
    end

    View full-size slide

  118. Let’s Learn Ruby
    Naming Convention

    View full-size slide

  119. Let’s Learn Ruby
    initialize

    View full-size slide

  120. Let’s Learn Ruby
    ClassName.new

    View full-size slide

  121. Let’s Learn Ruby
    self = current object

    View full-size slide

  122. Let’s Learn Ruby
    instance and class variable

    View full-size slide

  123. Let’s Learn Ruby
    instance and class method

    View full-size slide

  124. 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.

    View full-size slide

  125. Let’s Learn Ruby
    public, protected and
    private method

    View full-size slide

  126. Let’s Learn Ruby
    getter and setter

    View full-size slide

  127. Let’s Learn Ruby
    attr_reader, attr_writer and
    attr_accessor

    View full-size slide

  128. Let’s Learn Ruby
    Open Class

    View full-size slide

  129. Let’s Learn Ruby
    Module

    View full-size slide

  130. Let’s Learn Ruby
    module ModuleName
    ...
    end

    View full-size slide

  131. Let’s Learn Ruby
    module has no inheritance

    View full-size slide

  132. Let’s Learn Ruby
    module has no instance

    View full-size slide

  133. Let’s Learn Ruby
    Naming Convention

    View full-size slide

  134. Let’s Learn Ruby
    require v.s load

    View full-size slide

  135. Let’s Learn Ruby
    Priority?

    View full-size slide

  136. Let’s Learn Ruby
    Exercise
    please create a Bird class, which is also inherited
    from Animal class, but include a Fly module.

    View full-size slide

  137. Let’s Learn Ruby
    Mixin

    View full-size slide

  138. Let’s Learn Ruby
    Ruby is single inheritance

    View full-size slide

  139. Let’s Learn Ruby
    Duck Typing

    View full-size slide

  140. Let’s Learn Ruby
    include v.s extend

    View full-size slide

  141. Let’s Learn Ruby
    Bundle

    View full-size slide

  142. Let’s Learn Ruby
    Gemfile

    View full-size slide

  143. Let’s Learn Ruby
    http://rubygems.org/

    View full-size slide

  144. Let’s Learn Ruby
    gem "nokogiri", :git => "git://github.com/
    tenderlove/nokogiri.git"
    gem "secret_gem", :path => "~/my_secret_path"

    View full-size slide

  145. Let’s Learn Ruby
    bundle install

    View full-size slide

  146. Let’s Learn Ruby
    pack your own gem!

    View full-size slide

  147. 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/

    View full-size slide

  148. Let’s Learn Ruby
    Exercise
    please try to create a Gem spec with bundle
    command, modify, build and push to
    rubygems.org.

    View full-size slide

  149. Let’s Learn Ruby
    Rake

    View full-size slide

  150. 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

    View full-size slide

  151. Let’s Learn Ruby
    task :goto_toliet do
    puts "goto toliet"
    end
    !
    task :open_the_door => :goto_toliet do
    puts "open door"
    end

    View full-size slide

  152. Let’s Learn Ruby
    TDD

    View full-size slide

  153. 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

    View full-size slide

  154. 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

    View full-size slide

  155. Let’s Learn Ruby
    Ruby Koans
    http://rubykoans.com/

    View full-size slide

  156. Let’s Learn Ruby
    Ruby Object Model

    View full-size slide

  157. Let’s Learn Ruby
    At last..

    View full-size slide

  158. photo by redjar

    View full-size slide

  159. Let’s Learn Ruby
    pick up one scripting language

    View full-size slide

  160. photo by Quality & Style

    View full-size slide

  161. Let’s Learn Ruby
    @eddiekao
    https://www.ruby-lang.org/zh_tw/

    View full-size slide

  162. Let’s Learn Ruby
    Ruby is fun!

    View full-size slide

  163. Let’s Learn Ruby
    The only limitation is your
    imagination.

    View full-size slide

  164. ⾼高⾒見⻯⿓龍
    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

    View full-size slide