Click here to load reader

Why everyone likes ruby

  • View
    136

  • Download
    0

Embed Size (px)

Text of Why everyone likes ruby

  1. 1. ruby? , Deep Refactoring, 28.09.2016
  2. 2. ? ruby - , , , ruby - ruby , python*
  3. 3. ? ruby on rails 4 python Wat? https://www.destroyallsoftware.com/talks/wat
  4. 4. Ruby
  5. 5. class Foo def initialize puts 'hi' end end
  6. 6. class Foo def initialize puts 'hi' end end Foo.new # initialize, new?
  7. 7. irb(main):001:0> bool
  8. 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. 9. irb(main):002:0> "".to_b
  10. 10. irb(main):002:0> "".to_b NoMethodError: undefined method `to_b' for "":String from (irb):1 from /usr/bin/irb:12:in `' # , ?
  11. 11. irb(main):003:0> !!""
  12. 12. irb(main):003:0> !!"" (irb):2: warning: string literal in condition => true # warning? # ==
  13. 13. irb(main):003:0> !!"" (irb):2: warning: string literal in condition => true # warning? # == # true?
  14. 14. irb(main):004:0> !!{}
  15. 15. irb(main):004:0> !!{} => true
  16. 16. irb(main):004:0> !!{} => true irb(main):005:0> !![]
  17. 17. irb(main):004:0> !!{} => true irb(main):005:0> !![] => true
  18. 18. irb(main):004:0> !!{} => true irb(main):005:0> !![] => true irb(main):006:0> !!Object.new
  19. 19. irb(main):004:0> !!{} => true irb(main):005:0> !![] => true irb(main):006:0> !!Object.new => true # false, false/nil/ ==
  20. 20. # Ruby? DSL Rails-? irb(main):001:0> require 'active_support/all' => true
  21. 21. # Ruby? DSL Rails-? irb(main):001:0> require 'active_support/all' => true irb(main):002:0> [].blank?
  22. 22. # Ruby? DSL Rails-? irb(main):001:0> require 'active_support/all' => true irb(main):002:0> [].blank? => true
  23. 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. 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. 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. 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. 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. 28. # Python >>> arr = [1, 2, 3] >>> map(str, arr) ["1", "2", "3"]
  29. 29. irb(main):001:0> arr = [1, 2, 3] => [1, 2, 3]
  30. 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. 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. 32. irb(main):001:0> arr = ["1", "2", "3"] => ["1", "2", "3"]
  33. 33. irb(main):001:0> arr = ["1", "2", "3"] => ["1", "2", "3"] irb(main):002:0> arr.map(&:String.new)
  34. 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. 35. irb(main):003:0> arr.map(&String.method(:new))
  36. 36. irb(main):003:0> arr.map(&String.method(:new)) => ["1", "2", "3"] #
  37. 37. # chain.rb x = ' foo bar ' .strip .split(' ') puts x.to_s
  38. 38. # chain.rb x = ' foo bar ' .strip .split(' ') puts x.to_s irb(main):001:0> load './chain.rb' [1, 2] => nil
  39. 39. # chain.rb x = ' foo bar ' #.strip .split(' ') puts x.to_s
  40. 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. 41. irb(main):001:0> String.new "x"
  42. 42. irb(main):001:0> String.new "x" => "x"
  43. 43. irb(main):001:0> String.new "x" => "x" irb(main):002:0> String.new("x")
  44. 44. irb(main):001:0> String.new "x" => "x" irb(main):002:0> String.new("x") => "x"
  45. 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. 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. 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. 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. 49. irb(main):005:0> [String.new("x")]
  50. 50. irb(main):005:0> [String.new("x")] => ["x"]
  51. 51. irb(main):005:0> [String.new("x")] => ["x"] irb(main):006:0> [String.new ("x")]
  52. 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. 53. irb(main):001:0> %w(rm -rf /home)
  54. 54. irb(main):001:0> %w(rm -rf /home) => ["rm", "-rf", "/home"]
  55. 55. irb(main):001:0> %w(rm -rf /home) => ["rm", "-rf", "/home"] irb(main):002:0> %i(rm -rf /home)
  56. 56. irb(main):001:0> %w(rm -rf /home) => ["rm", "-rf", "/home"] irb(main):002:0> %i(rm -rf /home) => [:rm, :"-rf", :"/home"]
  57. 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. 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. 59. irb(main):001:0> {foo: 1} => {:foo=>1}
  60. 60. irb(main):001:0> {foo: 1} => {:foo=>1} irb(main):002:0> {:foo => 1} => {:foo=>1}
  61. 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. 62. irb(main):005:0> {:foo => 1}["foo"]
  63. 63. irb(main):005:0> {:foo => 1}["foo"] => nil
  64. 64. irb(main):005:0> {:foo => 1}["foo"] => nil irb(main):006:0> {:foo => 1}["foo".to_sym]
  65. 65. irb(main):005:0> {:foo => 1}["foo"] => nil irb(main):006:0> {:foo => 1}["foo".to_sym] => 1 #
  66. 66. irb(main):001:0> h = {:foo => 1} => {:foo=>1}
  67. 67. irb(main):001:0> h = {:foo => 1} => {:foo=>1} irb(main):002:0> h = h.with_indifferent_access => {"foo"=>1}
  68. 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. 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. 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. 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. 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. 73. def foo(&block) puts 'open resource' [*41..42].each &block.method(:call) puts 'close resource' end foo do |value| puts value end
  74. 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. 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. 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. 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. 78. def foo(&block) puts 'open resource' [*41..42].each &block.method(:call) puts 'c

Search related