Let's Learn Ruby - Basic

Download Let's Learn Ruby - Basic

Post on 06-May-2015

1.583 views

Category:

Technology

6 download

TRANSCRIPT

  • 1.Lets Learn Ruby - Basic

2. Ruby Tuesdayphoto by othreehttps://www.facebook.com/groups/142197385837507/ 3. RubyConf Taiwanhttp://rubyconf.tw/ 4. Rails Girls Taipeihttps://www.facebook.com/railsgirlstw 5. WebConf Taiwan 2014 6. 750+ attendees 7. all tickets sold out in 4 mins 8. Lets Learn RubyWhat I want? 9. Lets Learn RubyProblem Solving 10. Lets Learn RubyActive Ecosystem 11. Lets Learn RubyScenario 12. Lets Learn Rubyopen source projects on Github 13. Lets Learn RubyHistory 14. Lets Learn Ruby (Matz) 15. Lets Learn Ruby 16. Lets Learn Rubyrst released at 1995 17. Lets Learn Ruby2.0 released at 2013 18. Lets Learn Ruby2.1 released at 2013.12 19. Lets Learn RubyWhy Ruby?free, open source, easy to learn 20. Lets Learn RubyRuby != Rails 21. Lets Learn RubyHappy, and Fun 22. Lets Learn RubyRubiesCRuby(MRI), REE, mRuby, MacRuby,JRuby, IronRuby, Rubinius..etc 23. Lets Learn RubyVersion1.8, 1.9, 2.0, 2.1 24. Lets Learn RubyRuby 1.8 has no future 25. Lets Learn RubyRVMRuby Version Managerhttps://rvm.io/ 26. Lets Learn RubyEditorsVim, Emacs, Sublime Text... etc 27. Lets Learn Rubycoding stylehttps://github.com/styleguide/ruby 28. Lets Learn RubyBut Ruby is Slow..? 29. Lets Learn RubyWhat can Ruby do? 30. Lets Learn RubyRake Make, but Ruby version. Rack http://rake.rubyforge.org/ 31. Lets Learn RubyRackits a specication (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/ 32. Lets Learn Rubydeveloping MacOS and iOS app 33. Lets Learn Rubydrawing, image processing, music.. 34. Lets Learn RubyInstall Ruby now! 35. Lets Learn Rubyhttp://tryruby.org 36. Lets Learn RubyInteractive Ruby, irb 37. Lets Learn RubyGem 38. Lets Learn Rubygem install PACKAGE_NAME 39. Lets Learn Rubygem env 40. Lets Learn Rubygem list 41. Lets Learn RubyVariables and Constants 42. Lets Learn Rubylocal variable variable 43. Lets Learn Rubyglobal variable $variable 44. Lets Learn Rubyinstance variable @variable 45. Lets Learn Rubyclass variable @@variable 46. Lets Learn Rubyvirtual variable true, false, self, nil 47. Lets Learn Rubyvariable assignment a=1 x, y, z = 1, 2, 3 48. Lets Learn RubyConstantbegins with a capital letter,and it can be changed 49. Lets Learn RubyReserved word and Keyword 50. Lets Learn RubyReserved word and Keyword 51. Lets Learn RubyLogic and Flow Control 52. Lets Learn Rubyonly false and nil are false 53. Lets Learn Rubytrue v.s TrueClass false v.s FalseClass nil v.s NilClass 54. Lets Learn Rubyif..elsif..end 55. Lets Learn Rubyunless = not if 56. Lets Learn Rubyif modier 57. Lets Learn Rubycase .. when.. 58. Lets Learn RubyBEGIN{} and END{} 59. Lets Learn Rubya = true ? 'a' : 'b' 60. Lets Learn Rubya ||= 'a' 61. Lets Learn RubyComment # single line 62. Lets Learn RubyComment=begin .. =end 63. Lets Learn RubyLoop and Iteration 64. Lets Learn Rubyfor.. in.. 65. Lets Learn Rubywhile .. end 66. Lets Learn Rubyuntil .. end 67. Lets Learn Rubyuntil = not while 68. Lets Learn Rubytimes 69. Lets Learn Rubyupto, downto 70. Lets Learn Rubyeach, each_with_index 71. Lets Learn RubyIntegerhttp://www.ruby-doc.org/core-2.1.0/Integer.html 72. Lets Learn RubyFixnum and Bignum 73. Lets Learn Ruby10 / 3 74. Lets Learn RubyStringhttp://ruby-doc.org/core-2.1.0/String.html 75. Lets Learn Rubysingle and double quotes 76. Lets Learn Ruby%q v.s %Q 77. Lets Learn Ruby"%s" % "eddie" 78. Lets Learn Rubystring interpolation 79. Lets Learn RubyExercise please calculate how many characters and words of a section of a random article with Ruby. 80. Lets Learn RubyExerciseplease convert string abcdefg to gfedcba without using String#reverse method. 81. Lets Learn RubyArrayhttp://ruby-doc.org/core-2.1.0/Array.html 82. Lets Learn RubyArray.new v.s [] 83. Lets Learn Ruby%w 84. Lets Learn RubyExerciseplease sort a given array [1, 3, 4, 1, 7, nil, 7], and remove nil and duplicate number. 85. Lets Learn RubyExerciseplease covert a given array [1, 2, 3, 4, 5] to [1, 3, 5, 7, 9] with Array#map method. 86. Lets Learn RubyExerciseplease draw 5 unique random number between 1 to 52. 87. Lets Learn RubyHashhttp://ruby-doc.org/core-2.1.0/Hash.html 88. Lets Learn RubyHash.new v.s {} 89. Lets Learn Rubya = { :name => 'eddie' } a = { name: 'eddie' } 90. Lets Learn RubyRangehttp://ruby-doc.org/core-2.1.0/Range.html 91. Lets Learn Ruby(1..10) v.s (1...10) 92. Lets Learn RubyExerciseplease calculate the sum from 1 to 100 with Range. 93. Lets Learn RubyMethods 94. Lets Learn Rubydef method_name(param) ... end 95. Lets Learn Rubyparentheses can be omitted 96. Lets Learn Ruby? and ! 97. Lets Learn Rubyreturn value 98. Lets Learn RubySingleton Method 99. Lets Learn Rubyclass Cat def walk puts "I'm walking" end end !cat = Cat.newdef cat.y puts "I can y" endcat.y 100. Lets Learn RubyMethod Missing 101. Lets Learn Rubydef method_missing(method_name) puts "method: #{method_name} is called!" end !something_not_exists() 102. Lets Learn RubyException Handlingbegin .. rescue.. else.. ensure.. end 103. Lets Learn Rubydef open_my_le(le_name) File.open le_name do |f| puts f.read end endbegin open_my_le("block_demo.r") rescue => e puts e else puts "it's working good!" ensure puts "this must be executed, no matter what" end 104. Lets Learn RubyBlock 105. Lets Learn RubyProc 106. Lets Learn Rubymy_square = Proc.new { | x | x ** 2 } !# how to call a proc puts my_square.call(10) puts my_square[10] puts my_square.(10) puts my_square === 10# # # #100 100 100 100 107. Lets Learn Rubylambda, -> 108. Lets Learn Rubymy_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) puts my_lambda[10] puts my_lambda.(10) puts my_lambda === 10# # # #100 100 100 100 109. Lets Learn RubyProc v.s lambda 110. Lets Learn Rubydef 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 111. Lets Learn Ruby{} v.s do..endhttp://blog.eddie.com.tw/2011/06/03/do-end-vs-braces/ 112. Lets Learn RubyYield 113. Lets Learn RubyObject-Oriented Programming 114. Lets Learn Rubyeverything in Ruby is an Object 115. Lets Learn Rubyobject = state+ behavior 116. Lets Learn Rubyroot class = Objectroot class would be BasicObject in Ruby 1.9 117. Lets Learn Rubyclass ClassName < ParentClass ... end 118. Lets Learn RubyNaming Convention 119. Lets Learn Rubyinitialize 120. Lets Learn RubyClassName.new 121. Lets Learn Rubyself = current object 122. Lets Learn Rubyinstance and class variable 123. Lets Learn Rubyinstance and class method 124. Lets Learn RubyExercise please create a Dog class and Cat class, which are both inherited from Animal class, and implement walk and eat methods. 125. Lets Learn Rubypublic, protected and private method 126. Lets Learn Rubygetter and setter 127. Lets Learn Rubyattr_reader, attr_writer and attr_accessor 128. Lets Learn RubyOpen Class 129. Lets Learn RubyModule 130. Lets Learn Rubymodule ModuleName ... end 131. Lets Learn Rubymodule has no inheritance 132. Lets Learn Rubymodule has no instance 133. Lets Learn RubyNaming Convention 134. Lets Learn Rubyrequire v.s load 135. Lets Learn RubyPriority? 136. Lets Learn RubyExercise please create a Bird class, which is also inherited from Animal class, but include a Fly module. 137. Lets Learn RubyMixin 138. Lets Learn RubyRuby is single inheritance 139. Lets Learn RubyDuck Typing 140. Lets Learn Rubyinclude v.s extend 141. Lets Learn RubyBundle 142. Lets Learn RubyGemle 143. Lets Learn Rubyhttp://rubygems.org/ 144. Lets Learn Rubygem "nokogiri", :git => "git://github.com/ tenderlove/nokogiri.git" gem "secret_gem", :path => "~/my_secret_path" 145. Lets Learn Rubybundle install 146. Lets Learn Rubypack your own gem! 147. Lets Learn Ruby1. bundle gem NEW_NAME 2. gem build NEW_NAME.gemspec 3. gem push NEW_NAME.gemhttp://guides.rubygems.org/make-your-own-gem/ 148. Lets Learn RubyExercise please try to create a Gem spec with bundle command, modify, build and push to rubygems.org. 149. Lets Learn RubyRake 150. Lets Learn Rubydesc "mail sender" task :sendmail do puts "grap mailing list from database..." sleep 3 puts "mail sending..." sleep 3 puts "done!" end 151. Lets Learn Rubytask :goto_toliet do puts "goto toliet" end !task :open_the_door => :goto_toliet do puts "open door" end 152. Lets Learn RubyTDD 153. Lets Learn Rubyrequire 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 154. Lets Learn Rubyrequire "minitest/autorun"describe "test my bmi calculator" do it "should calc the correct bmi" do calc_bmi(175, 80).must_equal 26.12 end enddef calc_bmi(height, weight) bmi = ( weight / (height/100.0) ** 2 ).round(2) end 155. Lets Learn RubyRuby Koanshttp://rubykoans.com/ 156. Lets Learn RubyRuby Object Model 157. Lets Learn RubyAt last.. 158. photo by redjar 159. Lets Learn Rubypick up one scripting language 160. photo by Quality & Style 161. Lets Learn Ruby@eddiekaohttps://www.ruby-lang.org/zh_tw/ 162. Lets Learn RubyRuby is fun! 163. Lets Learn RubyThe only limitation is your imagination. 164. Contacts Websitehttp://www.eddie.com.twBloghttp://blog.eddie.com.twPlurkhttp://www.plurk.com/aquarianboyFacebookhttp://www.facebook.com/eddiekaoGoogle Plushttp://www.eddie.com.tw/+Twitterhttps://twitter.com/eddiekaoEmaileddie@digik.com.twMobile+886-928-617-687photo by Eddie