Ruby for Starter

Ruby for Starter

F2dcf6633971844e19ca96ea294ba976?s=128

高見龍

March 23, 2013
Tweet

Transcript

  1. Ruby Starter eddie@fju

  2. None
  3. current status

  4. 80% iOS app, 20% Ruby/Rails

  5. What I want?

  6. Problem Solving

  7. History

  8. まつもと ゆきひろ (Matz)

  9. first released at 1995

  10. 2.0 released at 2013

  11. None
  12. Why Ruby? free, open source, easy to learn

  13. Ruby != Rails

  14. Happy, and Fun

  15. Rubies CRuby(MRI), REE, mRuby, JRuby, IronRuby, Rubinius..etc

  16. Version 1.8, 1.9, 2.0

  17. Ruby 1.8 has no future

  18. RVM Ruby Version Manager https://rvm.io/

  19. Editors Vim, Emacs, TextMate, Sublime Text... etc

  20. git

  21. coding style https://github.com/styleguide/ruby

  22. Variables and Constants

  23. local variable variable

  24. global variable $variable

  25. instance variable @variable

  26. class variable @@variable

  27. virtual variable true, false, self, nil

  28. variable assignment a = 1 x, y, z = 1,

    2, 3
  29. Constant begins with a capital letter, and it can be

    changed
  30. Reserved word and Keyword

  31. Logic and Flow Control

  32. only false and nil are false

  33. true v.s. TrueClass false v.s. FalseClass nil v.s. NilClass

  34. if..elsif..end

  35. unless = if not

  36. if modifier

  37. case .. when..

  38. BEGIN{} and END{}

  39. a = true ? 'a' : 'b'

  40. a ||= 'a'

  41. Comment # single line

  42. Comment =begin .. =end

  43. Loop and Iteration

  44. for.. in..

  45. while .. end

  46. until .. end

  47. until = while not

  48. times

  49. upto, downto

  50. each, each_with_index

  51. Block

  52. Proc

  53. my_square = Proc.new { | x | x ** 2

    } my_square.call(10) # 100 my_square[10] # 100
  54. lambda, ->

  55. my_lambda = lambda { | x | x ** 2

    } # new style in 1.9 my_lambda = -> x { x ** 2 } # how to call a lambda? my_lambda.call(10) my_lambda[10]
  56. Proc v.s. lambda

  57. 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
  58. {} v.s. do..end http://blog.eddie.com.tw/2011/06/03/do-end-vs-braces/

  59. Number

  60. Fixnum and Bignum

  61. 10 / 3

  62. String http://ruby-doc.org/core-1.9.2/String.html

  63. single and double quotes

  64. %q v.s. %Q

  65. "%s" % "eddie"

  66. string interpolation

  67. Array http://ruby-doc.org/core-1.9.2/Array.html

  68. Array.new v.s. []

  69. %w

  70. Hash http://ruby-doc.org/core-1.9.2/Hash.html

  71. Hash.new v.s {}

  72. a = { :name => 'eddie' } a = {

    name: 'eddie' }
  73. Range http://ruby-doc.org/core-1.9.2/Range.html

  74. (1..10) v.s. (1...10)

  75. Methods

  76. def method_name(param) ... end

  77. parentheses can be omitted

  78. ? and !

  79. return value

  80. Singleton Method

  81. class Cat def walk puts "I'm walking" end end cat

    = Cat.new def cat.fly puts "I can fly" end cat.fly
  82. Method Missing

  83. def method_missing(method_name) puts "method: #{method_name} is called!" end [1, 2,

    3, 4].hello
  84. Exception Handling begin .. rescue.. else.. ensure.. end

  85. 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
  86. Object-Oriented Programming

  87. everything in Ruby is an Object

  88. object = state+ behavior

  89. top class = Object top class would be BasicObject in

    Ruby 1.9
  90. class ClassName < ParentClass ... end

  91. Naming Convention

  92. initialize

  93. ClassName.new

  94. self = current object

  95. instance and class variable

  96. instance and class method

  97. public, protected and private method

  98. getter and setter

  99. attr_reader, attr_writer and attr_accessor

  100. Open Class

  101. Module

  102. module ModuleName ... end

  103. module has no inheritance

  104. module has no instance

  105. Naming Convention

  106. require v.s. load

  107. Mixin

  108. Ruby is single inheritance

  109. Duck Typing

  110. include v.s. extend

  111. Gem

  112. gem install PACKAGE_NAME

  113. gem env

  114. Bundle

  115. Gemfile

  116. gem "nokogiri", :git => "git://github.com/tenderlove/nokogiri.git" gem "secret_gem", :path => "~/my_secret_path"

  117. bundle install

  118. pack your own gem!

  119. 1. bundle gem NEW_NAME 2. gem build NEW_NAME.gemspec 3. gem

    push NEW_NAME.gem http://docs.rubygems.org/read/chapter/20
  120. Rake

  121. Ruby Object Model