Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

1

Share

Download to read offline

Why everyone likes ruby

Download to read offline

Describing several cases where language behaves different to common standards and breaks expectations

Related Books

Free with a 30 day trial from Scribd

See all

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 `<main>' # Ок, это же язык с продвинутым ООП
  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 `<main>' # Где метод у объекта, это же ООП язык?
  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 `<main>' # это было бы слишком элегантно
  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 `<main>' # даже 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 `<main>' # Пробелы
  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 'close resource' end def has_answer? foo do |value| return true if value == 42 end end puts has_answer? > open resource true
  79. 79. def foo(&block) puts 'open resource' [*41..42].each &block.method(:call) puts 'close resource' end def has_answer? rc = false foo {|value| rc = true if value == 42} rc end puts has_answer?
  80. 80. def foo(&block) puts 'open resource' [*41..42].each &block.method(:call) puts 'close resource' end def has_answer? rc = false foo {|value| rc = true if value == 42} rc end puts has_answer? > open resource close resource true
  81. 81. module Foo def bar puts 'hi' end end irb(main):009:0* Foo.bar
  82. 82. module Foo def bar puts 'hi' end end irb(main):009:0* Foo.bar NoMethodError: undefined method `bar' for Foo:Module from (irb):9 from /usr/bin/irb:12:in `<main>'
  83. 83. module Foo module_function def bar puts 'hi' end end irb(main):010:0* Foo.bar hi
  84. 84. module Foo BAZ = 'hey' module_function def bar puts 'hi' end end
  85. 85. module Foo BAZ = 'hey' module_function def bar puts 'hi' end end irb(main):096:0> Foo::bar
  86. 86. module Foo BAZ = 'hey' module_function def bar puts 'hi' end end irb(main):096:0> Foo::bar hi => nil
  87. 87. module Foo BAZ = 'hey' module_function def bar puts 'hi' end end irb(main):096:0> Foo::bar hi => nil irb(main):097:0> Foo::BAZ
  88. 88. module Foo BAZ = 'hey' module_function def bar puts 'hi' end end irb(main):096:0> Foo::bar hi => nil irb(main):097:0> Foo::BAZ => "hey"
  89. 89. module Foo BAZ = 'hey' module_function def bar puts 'hi' end end irb(main):096:0> Foo::bar hi => nil irb(main):097:0> Foo::BAZ => "hey" irb(main):098:0> Foo.baz
  90. 90. module Foo BAZ = 'hey' module_function def bar puts 'hi' end end irb(main):096:0> Foo::bar hi => nil irb(main):097:0> Foo::BAZ => "hey" irb(main):098:0> Foo.baz NoMethodError: undefined method `baz' for Foo:Module from (irb):98 from /usr/bin/irb:12:in `<main>' # почему нет?
  91. 91. В качестве заключения, что с популярностью ruby?
  92. 92. http://githut.info/
  93. 93. http://pypl.github.io/PYPL.html
  94. 94. http://stackoverflow.com/jobs
  95. 95. Спасибо за внимание
  • RyotaYoshiyama

    Nov. 22, 2016

Describing several cases where language behaves different to common standards and breaks expectations

Views

Total views

259

On Slideshare

0

From embeds

0

Number of embeds

73

Actions

Downloads

4

Shares

0

Comments

0

Likes

1

×