Click here to load reader
Why everyone likes ruby
Embed Size (px)
344 x 292 429 x 357 514 x 422 599 x 487
Text of Why everyone likes ruby 1. ruby? , Deep Refactoring, 28.09.2016 2. ? ruby - , , , ruby - ruby , python* 3. ? ruby on rails 4 python Wat? https://www.destroyallsoftware.com/talks/wat 4. Ruby 5. class Foo def initialize puts 'hi' end end 6. class Foo def initialize puts 'hi' end end Foo.new # initialize, new? 7. irb(main):001:0> bool 8. irb(main):001:0> bool NameError: undefined local variable or method `bool' for main:Object from (irb):1 from /usr/bin/irb:12:in `' # , 9. irb(main):002:0> "".to_b 10. irb(main):002:0> "".to_b NoMethodError: undefined method `to_b' for "":String from (irb):1 from /usr/bin/irb:12:in `' # , ? 11. irb(main):003:0> !!"" 12. irb(main):003:0> !!"" (irb):2: warning: string literal in condition => true # warning? # == 13. irb(main):003:0> !!"" (irb):2: warning: string literal in condition => true # warning? # == # true? 14. irb(main):004:0> !!{} 15. irb(main):004:0> !!{} => true 16. irb(main):004:0> !!{} => true irb(main):005:0> !![] 17. irb(main):004:0> !!{} => true irb(main):005:0> !![] => true 18. irb(main):004:0> !!{} => true irb(main):005:0> !![] => true irb(main):006:0> !!Object.new 19. irb(main):004:0> !!{} => true irb(main):005:0> !![] => true irb(main):006:0> !!Object.new => true # false, false/nil/ == 20. # Ruby? DSL Rails-? irb(main):001:0> require 'active_support/all' => true 21. # Ruby? DSL Rails-? irb(main):001:0> require 'active_support/all' => true irb(main):002:0> [].blank? 22. # Ruby? DSL Rails-? irb(main):001:0> require 'active_support/all' => true irb(main):002:0> [].blank? => true 23. # Ruby? DSL Rails-? irb(main):001:0> require 'active_support/all' => true irb(main):002:0> [].blank? => true irb(main):003:0> nil.blank? 24. # Ruby? DSL Rails-? irb(main):001:0> require 'active_support/all' => true irb(main):002:0> [].blank? => true irb(main):003:0> nil.blank? => true 25. # Ruby? DSL Rails-? irb(main):001:0> require 'active_support/all' => true irb(main):002:0> [].blank? => true irb(main):003:0> nil.blank? => true irb(main):004:0> 0.blank? 26. # Ruby? DSL Rails-? irb(main):001:0> require 'active_support/all' => true irb(main):002:0> [].blank? => true irb(main):003:0> nil.blank? => true irb(main):004:0> 0.blank? => false 27. # Ruby? DSL Rails-? irb(main):001:0> require 'active_support/all' => true irb(main):002:0> [].blank? => true irb(main):003:0> nil.blank? => true irb(main):004:0> 0.blank? => false # 28. # Python >>> arr = [1, 2, 3] >>> map(str, arr) ["1", "2", "3"] 29. irb(main):001:0> arr = [1, 2, 3] => [1, 2, 3] 30. irb(main):001:0> arr = [1, 2, 3] => [1, 2, 3] irb(main):002:0> arr.map {|item| item.to_s} => ["1", "2", "3"] 31. irb(main):001:0> arr = [1, 2, 3] => [1, 2, 3] irb(main):002:0> arr.map {|item| item.to_s} => ["1", "2", "3"] irb(main):003:0> arr.map(&:to_s) => ["1", "2", "3"] # &: - 32. irb(main):001:0> arr = ["1", "2", "3"] => ["1", "2", "3"] 33. irb(main):001:0> arr = ["1", "2", "3"] => ["1", "2", "3"] irb(main):002:0> arr.map(&:String.new) 34. irb(main):001:0> arr = ["1", "2", "3"] => ["1", "2", "3"] irb(main):002:0> arr.map(&:String.new) NoMethodError: undefined method `new' for :String:Symbol from (irb):26 from /usr/bin/irb:12:in `' # 35. irb(main):003:0> arr.map(&String.method(:new)) 36. irb(main):003:0> arr.map(&String.method(:new)) => ["1", "2", "3"] # 37. # chain.rb x = ' foo bar ' .strip .split(' ') puts x.to_s 38. # chain.rb x = ' foo bar ' .strip .split(' ') puts x.to_s irb(main):001:0> load './chain.rb' [1, 2] => nil 39. # chain.rb x = ' foo bar ' #.strip .split(' ') puts x.to_s 40. irb(main):001:0> load './chain.rb' SyntaxError: /tmp/code.rb:4: syntax error, unexpected '.', expecting end-of-input .split(' ') ^ from (irb):15:in `load' from (irb):15 from /usr/bin/irb:12:in `' # python 41. irb(main):001:0> String.new "x" 42. irb(main):001:0> String.new "x" => "x" 43. irb(main):001:0> String.new "x" => "x" irb(main):002:0> String.new("x") 44. irb(main):001:0> String.new "x" => "x" irb(main):002:0> String.new("x") => "x" 45. irb(main):001:0> String.new "x" => "x" irb(main):002:0> String.new("x") => "x" irb(main):003:0> String.new( "x" ) 46. irb(main):001:0> String.new "x" => "x" irb(main):002:0> String.new("x") => "x" irb(main):003:0> String.new( "x" ) => "x" 47. irb(main):001:0> String.new "x" => "x" irb(main):002:0> String.new("x") => "x" irb(main):003:0> String.new( "x" ) => "x" irb(main):004:0> String.new ("x") 48. irb(main):001:0> String.new "x" => "x" irb(main):002:0> String.new("x") => "x" irb(main):003:0> String.new( "x" ) => "x" irb(main):004:0> String.new ("x") => "x" 49. irb(main):005:0> [String.new("x")] 50. irb(main):005:0> [String.new("x")] => ["x"] 51. irb(main):005:0> [String.new("x")] => ["x"] irb(main):006:0> [String.new ("x")] 52. irb(main):005:0> [String.new("x")] => ["x"] irb(main):006:0> [String.new ("x")] SyntaxError: (irb):25: syntax error, unexpected ( arg, expecting ']' [String.new ("x")] ^ (irb):25: syntax error, unexpected ']', expecting end-of-input from /usr/bin/irb:12:in `' # 53. irb(main):001:0> %w(rm -rf /home) 54. irb(main):001:0> %w(rm -rf /home) => ["rm", "-rf", "/home"] 55. irb(main):001:0> %w(rm -rf /home) => ["rm", "-rf", "/home"] irb(main):002:0> %i(rm -rf /home) 56. irb(main):001:0> %w(rm -rf /home) => ["rm", "-rf", "/home"] irb(main):002:0> %i(rm -rf /home) => [:rm, :"-rf", :"/home"] 57. irb(main):001:0> %w(rm -rf /home) => ["rm", "-rf", "/home"] irb(main):002:0> %i(rm -rf /home) => [:rm, :"-rf", :"/home"] irb(main):003:0> %x(rm -rf /home) 58. irb(main):001:0> %w(rm -rf /home) => ["rm", "-rf", "/home"] irb(main):002:0> %i(rm -rf /home) => [:rm, :"-rf", :"/home"] irb(main):003:0> %x(rm -rf /home) => "" # OSX 59. irb(main):001:0> {foo: 1} => {:foo=>1} 60. irb(main):001:0> {foo: 1} => {:foo=>1} irb(main):002:0> {:foo => 1} => {:foo=>1} 61. irb(main):001:0> {foo: 1} => {:foo=>1} irb(main):002:0> {:foo => 1} => {:foo=>1} irb(main):003:0> {"foo" => 1} => {"foo"=>1} 62. irb(main):005:0> {:foo => 1}["foo"] 63. irb(main):005:0> {:foo => 1}["foo"] => nil 64. irb(main):005:0> {:foo => 1}["foo"] => nil irb(main):006:0> {:foo => 1}["foo".to_sym] 65. irb(main):005:0> {:foo => 1}["foo"] => nil irb(main):006:0> {:foo => 1}["foo".to_sym] => 1 # 66. irb(main):001:0> h = {:foo => 1} => {:foo=>1} 67. irb(main):001:0> h = {:foo => 1} => {:foo=>1} irb(main):002:0> h = h.with_indifferent_access => {"foo"=>1} 68. irb(main):001:0> h = {:foo => 1} => {:foo=>1} irb(main):002:0> h = h.with_indifferent_access => {"foo"=>1} irb(main):003:0> h["foo"] 69. irb(main):001:0> h = {:foo => 1} => {:foo=>1} irb(main):002:0> h = h.with_indifferent_access => {"foo"=>1} irb(main):003:0> h["foo"] => 1 70. irb(main):001:0> h = {:foo => 1} => {:foo=>1} irb(main):002:0> h = h.with_indifferent_access => {"foo"=>1} irb(main):003:0> h["foo"] => 1 irb(main):004:0> h[:foo] 71. irb(main):001:0> h = {:foo => 1} => {:foo=>1} irb(main):002:0> h = h.with_indifferent_access => {"foo"=>1} irb(main):003:0> h["foo"] => 1 irb(main):004:0> h[:foo] => 1 # rails 72. irb(main):001:0> h = {:foo => 1} => {:foo=>1} irb(main):002:0> h = h.with_indifferent_access => {"foo"=>1} irb(main):003:0> h["foo"] => 1 irb(main):004:0> h[:foo] => 1 # rails # ruby DSL rails 73. def foo(&block) puts 'open resource' [*41..42].each &block.method(:call) puts 'close resource' end foo do |value| puts value end 74. def foo(&block) puts 'open resource' [*41..42].each &block.method(:call) puts 'close resource' end foo do |value| puts value end > open resource 41 42 close resource 75. def foo(&block) puts 'open resource' [*41..42].each &block.method(:call) puts 'close resource' end foo do |value| puts value break end 76. def foo(&block) puts 'open resource' [*41..42].each &block.method(:call) puts 'close resource' end foo do |value| puts value break end > open resource 41 77. def foo(&block) puts 'open resource' [*41..42].each &block.method(:call) puts 'close resource' end def has_answer? foo do |value| return true if value == 42 end end puts has_answer? 78. def foo(&block) puts 'open resource' [*41..42].each &block.method(:call) puts 'c