Stupid Ruby Tricks

Cd8da976054ea4915eafc5d9dd096d38?s=47 blowmage
March 07, 2015

Stupid Ruby Tricks

Ruby is awesome. We all love Ruby. And Ruby loves us. We shouldn't abuse Ruby. Well, maybe a little.

Cd8da976054ea4915eafc5d9dd096d38?s=128

blowmage

March 07, 2015
Tweet

Transcript

  1. Stupid Ruby Tricks Stupid Ruby Tricks Mike Moore @blowmage Mike

    Moore @blowmage
  2. None
  3. None
  4. Stupid Stupid

  5. None
  6. Mike Moore Mike Moore

  7. Mike Moore Mike Moore @blowmage @blowmage

  8. None
  9. None
  10. None
  11. None
  12. None
  13. None
  14. None
  15. None
  16. None
  17. None
  18. None
  19. Stupid Stupid

  20. Hey, can I move to Friday? I won’t be prepared

    in time. No problem. We’ll move Terence Lee to Thursday. Sweet!
  21. Hey, can I move to Friday? I won’t be prepared

    in time. No problem. We’ll move Terence Lee to Thursday. Sweet!
  22. Hey, can I move to Friday? I won’t be prepared

    in time. No problem. We’ll move Terence Lee to Thursday. Sweet!
  23. Hey, can I move to Friday? I won’t be prepared

    in time. No problem. We’ll move Terence Lee to Thursday. Sweet!
  24. Hey, can I move to Friday? I won’t be prepared

    in time. No problem. We’ll move Terence Lee to Thursday. Sweet! #FridayHug?!? #FridayHug?!?
  25. Hey, can I move to Friday? I won’t be prepared

    in time. No problem. We’ll move Terence Lee to Thursday. Sweet! #FridayHug?!? #FridayHug?!?
  26. Hey, can I move to Friday? I won’t be prepared

    in time. No problem. We’ll move Terence Lee to Thursday. Sweet! #FridayHug?!? #FridayHug?!?
  27. None
  28. None
  29. Its full of puns! Its full of puns!

  30. Stupid Stupid

  31. Stupid Ruby Trick #1 File.write/File.read Stupid Ruby Trick #1 File.write/File.read

  32. None
  33. File.open("stupid.txt", "w") do |file|
 file.write("Its all about closures, yo!")
 end


  34. File.open("stupid.txt", "w") do |file|
 file.write("Its all about closures, yo!")
 end


    
 message = File.open("stupid.txt") do |file|
 file.read
 end
 puts(message)
  35. None
  36. # File.open("stupid.txt", "w") do |file|
 # file.write("Its all about closures,

    yo!")
 # end
  37. # File.open("stupid.txt", "w") do |file|
 # file.write("Its all about closures,

    yo!")
 # end File.write("stupid.txt", "Inline, baby!")

  38. # File.open("stupid.txt", "w") do |file|
 # file.write("Its all about closures,

    yo!")
 # end File.write("stupid.txt", "Inline, baby!")
 # message = File.open("stupid.txt") do |file|
 # file.read
 # end
 # puts(message)
  39. # File.open("stupid.txt", "w") do |file|
 # file.write("Its all about closures,

    yo!")
 # end File.write("stupid.txt", "Inline, baby!")
 # message = File.open("stupid.txt") do |file|
 # file.read
 # end
 # puts(message) puts(File.read("stupid.txt"))
  40. Stupid Stupid

  41. Stupid Ruby Trick #2 Optional Parenthesis Stupid Ruby Trick #2

    Optional Parenthesis
  42. File.open("stupid.txt", "w") do |file|
 file.write("Its all about closures, yo!")
 end


    File.write("stupid.txt", "Inline, baby!")
 
 message = File.open("stupid.txt") do |file|
 file.read
 end
 puts(message)
 puts(File.read("stupid.txt"))
  43. File.open(”stupid.txt", "w") do |file|
 file.write("Its all about closures, yo!")
 end


    File.write("stupid.txt", "Inline, baby!")
 
 message = File.open("stupid.txt") do |file|
 file.read
 end
 puts(message)
 puts(File.read("stupid.txt"))
  44. File.open "stupid.txt", "w" do |file|
 file.write "Its all about closures,

    yo!"
 end
 File.write "stupid.txt", "Inline, baby!"
 
 message = File.open "stupid.txt" do |file|
 file.read
 end
 puts message
 puts File.read "stupid.txt"
  45. Stupid Ruby Advice #1 Don’t overload lines Stupid Ruby Advice

    #1 Don’t overload lines
  46. None
  47. None
  48. Stupid Stupid

  49. Stupid Ruby Trick #3 Default Hash Values Stupid Ruby Trick

    #3 Default Hash Values
  50. None
  51. h = { foo: "FOO", bar: "BAR" }

  52. h = { foo: "FOO", bar: "BAR" } h[:baz] #=>

    nil
  53. h = { foo: "FOO", bar: "BAR" } h[:baz] #=>

    nil h.fetch :baz, "BAZ" #=> “BAZ"
  54. h = { foo: "FOO", bar: "BAR" } h[:baz] #=>

    nil h.fetch :baz, "BAZ" #=> “BAZ" h[:baz] = "TROLLFACE"
  55. h = { foo: "FOO", bar: "BAR" } h[:baz] #=>

    nil h.fetch :baz, "BAZ" #=> “BAZ" h[:baz] = "TROLLFACE" h.fetch :baz, "BAZ" #=> "TROLLFACE"
  56. None
  57. hash_list = Hash.new []

  58. hash_list = Hash.new [] hash_list[:nope]

  59. hash_list = Hash.new [] hash_list[:nope] hash_list[:nope] << :x

  60. hash_list = Hash.new [] hash_list[:nope] hash_list[:nope] << :x hash_list[:nope] <<

    :y
  61. hash_list = Hash.new [] hash_list[:nope] hash_list[:nope] << :x hash_list[:nope] <<

    :y hash_list[:nope] << :z
  62. hash_list = Hash.new [] hash_list[:nope] hash_list[:nope] << :x hash_list[:nope] <<

    :y hash_list[:nope] << :z hash_list[:nope] #=> [:x, :y, :z]
  63. hash_list = Hash.new [] hash_list[:nope] hash_list[:nope] << :x hash_list[:nope] <<

    :y hash_list[:nope] << :z hash_list[:nope] #=> [:x, :y, :z] hash_list[:not_even] #=> [:x, :y, :z]
  64. hash_list = Hash.new Array.new
 hash_list[:nope]
 hash_list[:nope] << :x
 hash_list[:nope] <<

    :y
 hash_list[:nope] << :z
 hash_list[:nope] #=> [:x, :y, :z]
 hash_list[:not_even] #=> [:x, :y, :z]
  65. None
  66. default #=> "default_value"
 h = Hash.new default


  67. default #=> "default_value"
 h = Hash.new default
 h[:nope] #=> "default_value"


  68. default #=> "default_value"
 h = Hash.new default
 h[:nope] #=> "default_value"


    default.upcase!

  69. default #=> "default_value"
 h = Hash.new default
 h[:nope] #=> "default_value"


    default.upcase!
 h[:nope] #=> "DEFAULT_VALUE"
  70. None
  71. hash_list = Hash.new { |h, k| h[k] = [] }

  72. hash_list = Hash.new { |h, k| h[k] = [] }

    hash_list[:nope]
  73. hash_list = Hash.new { |h, k| h[k] = [] }

    hash_list[:nope] hash_list[:nope] << :x
 hash_list[:nope] << :y
 hash_list[:nope] << :z
  74. hash_list = Hash.new { |h, k| h[k] = [] }

    hash_list[:nope] hash_list[:nope] << :x
 hash_list[:nope] << :y
 hash_list[:nope] << :z hash_list[:nope] #=> [:x, :y, :z]
  75. hash_list = Hash.new { |h, k| h[k] = [] }

    hash_list[:nope] hash_list[:nope] << :x
 hash_list[:nope] << :y
 hash_list[:nope] << :z hash_list[:nope] #=> [:x, :y, :z] hash_list[:not_even] #=> []
  76. None
  77. hash_recurs = Hash.new { |h, k| h[k] = {} }

  78. hash_recurs = Hash.new { |h, k| h[k] = {} }

    hash_recurs #=> {}
  79. hash_recurs = Hash.new { |h, k| h[k] = {} }

    hash_recurs #=> {} hash_recurs[:x] #=> {}
  80. hash_recurs = Hash.new { |h, k| h[k] = {} }

    hash_recurs #=> {} hash_recurs[:x] #=> {} hash_recurs[:x][:y] #=> nil
  81. hash_recurs = Hash.new { |h, k| h[k] = {} }

    hash_recurs #=> {} hash_recurs[:x] #=> {} hash_recurs[:x][:y] #=> nil hash_recurs[:x][:y][:z]
 #=> NoMethodError: undefined method `[]' for nil:NilClass
  82. None
  83. hash_recurs = Hash.new do |h, k|
 h[k] = Hash.new &h.default_proc


    end
  84. hash_recurs = Hash.new do |h, k|
 h[k] = Hash.new &h.default_proc


    end hash_recurs[:x] #=> {}
  85. hash_recurs = Hash.new do |h, k|
 h[k] = Hash.new &h.default_proc


    end hash_recurs[:x] #=> {} hash_recurs[:x][:y] #=> {}
  86. hash_recurs = Hash.new do |h, k|
 h[k] = Hash.new &h.default_proc


    end hash_recurs[:x] #=> {} hash_recurs[:x][:y] #=> {} hash_recurs[:x][:y][:z] #=> {}
  87. hash_recurs = Hash.new do |h, k|
 h[k] = Hash.new &h.default_proc


    end hash_recurs[:x] #=> {} hash_recurs[:x][:y] #=> {} hash_recurs[:x][:y][:z] #=> {} hash_recurs #=> {:x=>{:y=>{:z=>{}}}}
  88. None
  89. frankenstein = Hash.new { |h, k| h[k] = [] }

  90. frankenstein = Hash.new { |h, k| h[k] = [] }

    frankenstein[:nope] << :x << :y << :z
  91. frankenstein = Hash.new { |h, k| h[k] = [] }

    frankenstein[:nope] << :x << :y << :z frankenstein[:nope] #=> [:x, :y, :z]
  92. frankenstein = Hash.new { |h, k| h[k] = [] }

    frankenstein[:nope] << :x << :y << :z frankenstein[:nope] #=> [:x, :y, :z] frankenstein[:not_even] #=> []
  93. frankenstein = Hash.new { |h, k| h[k] = [] }

    frankenstein[:nope] << :x << :y << :z frankenstein[:nope] #=> [:x, :y, :z] frankenstein[:not_even] #=> [] frankenstein #=> {:nope=>[:x, :y, :z], :not_even=>[]}
  94. frankenstein = Hash.new { |h, k| h[k] = [] }

    frankenstein[:nope] << :x << :y << :z frankenstein[:nope] #=> [:x, :y, :z] frankenstein[:not_even] #=> [] frankenstein #=> {:nope=>[:x, :y, :z], :not_even=>[]} frankenstein.default_proc = ->(h, k) do
 h[k] = Hash.new &h.default_proc
 end
  95. frankenstein = Hash.new { |h, k| h[k] = [] }

    frankenstein[:nope] << :x << :y << :z frankenstein[:nope] #=> [:x, :y, :z] frankenstein[:not_even] #=> [] frankenstein #=> {:nope=>[:x, :y, :z], :not_even=>[]} frankenstein.default_proc = ->(h, k) do
 h[k] = Hash.new &h.default_proc
 end frankenstein[:x][:y][:z] #=> {}
  96. frankenstein = Hash.new { |h, k| h[k] = [] }

    frankenstein[:nope] << :x << :y << :z frankenstein[:nope] #=> [:x, :y, :z] frankenstein[:not_even] #=> [] frankenstein #=> {:nope=>[:x, :y, :z], :not_even=>[]} frankenstein.default_proc = ->(h, k) do
 h[k] = Hash.new &h.default_proc
 end frankenstein[:x][:y][:z] #=> {} frankenstein
 #=> {:nope=>[:x, :y, :z], :not_even=>[],
 :x=>{:y=>{:z=>{}}}}
  97. None
  98. Stupid Stupid

  99. Stupid Ruby Trick #4 include vs. prepend and super Stupid

    Ruby Trick #4 include vs. prepend and super
  100. module Foo
 def do_the_thing
 puts "Foo!"
 end
 end
 class Me


    include Foo
 
 def do_the_thing
 puts "Me!"
 end
 end

  101. module Foo
 def do_the_thing
 puts "Foo!"
 end
 end
 class Me


    include Foo
 
 def do_the_thing
 puts "Me!"
 end
 end
 Me.new.do_the_thing
  102. module Foo
 def do_the_thing
 puts "Foo!"
 end
 end
 class Me


    include Foo
 
 def do_the_thing
 puts "Me!"
 end
 end
 Me.new.do_the_thing "Me!"
  103. module Foo
 def do_the_thing
 puts "Foo!"
 end
 end
 class Me


    include Foo
 
 def do_the_thing
 puts "Me!"
 super
 end
 end
  104. module Foo
 def do_the_thing
 puts "Foo!"
 end
 end
 class Me


    include Foo
 
 def do_the_thing
 puts "Me!"
 super
 end
 end Me.new.do_the_thing
  105. module Foo
 def do_the_thing
 puts "Foo!"
 end
 end
 class Me


    include Foo
 
 def do_the_thing
 puts "Me!"
 super
 end
 end Me.new.do_the_thing "Me!"
 "Foo!"
  106. module Foo
 def do_the_thing
 puts "Foo!"
 end
 end
 
 class

    Me
 prepend Foo
 
 def do_the_thing
 puts "Me!"
 super
 end
 end
  107. module Foo
 def do_the_thing
 puts "Foo!"
 end
 end
 
 class

    Me
 prepend Foo
 
 def do_the_thing
 puts "Me!"
 super
 end
 end Me.new.do_the_thing
  108. module Foo
 def do_the_thing
 puts "Foo!"
 end
 end
 
 class

    Me
 prepend Foo
 
 def do_the_thing
 puts "Me!"
 super
 end
 end Me.new.do_the_thing "Foo!"
  109. module Foo
 def do_the_thing
 puts "Foo!"
 super
 end
 end
 


    class Me
 prepend Foo
 
 def do_the_thing
 puts "Me!"
 super
 end
 end
  110. module Foo
 def do_the_thing
 puts "Foo!"
 super
 end
 end
 


    class Me
 prepend Foo
 
 def do_the_thing
 puts "Me!"
 super
 end
 end Me.new.do_the_thing
  111. module Foo
 def do_the_thing
 puts "Foo!"
 super
 end
 end
 


    class Me
 prepend Foo
 
 def do_the_thing
 puts "Me!"
 super
 end
 end Me.new.do_the_thing "Foo!"
 "Me!"
 #=> NoMethodError: super: no superclass method `do_the_thing' for #<Me:0x007ff7fe688d70>
  112. module Foo
 def do_the_thing
 puts "Foo!"
 super if defined? super


    end
 end
 
 class Me
 prepend Foo
 
 def do_the_thing
 puts "Me!"
 super if defined? super
 end
 end
  113. module Foo
 def do_the_thing
 puts "Foo!"
 super if defined? super


    end
 end
 
 class Me
 prepend Foo
 
 def do_the_thing
 puts "Me!"
 super if defined? super
 end
 end Me.new.do_the_thing
  114. module Foo
 def do_the_thing
 puts "Foo!"
 super if defined? super


    end
 end
 
 class Me
 prepend Foo
 
 def do_the_thing
 puts "Me!"
 super if defined? super
 end
 end Me.new.do_the_thing "Foo!"
 "Me!"
  115. module Foo
 def do_the_thing
 puts "Foo!"
 super if defined? super


    end
 end
 
 class Me
 include Foo
 
 def do_the_thing
 puts "Me!"
 super if defined? super
 end
 end
  116. module Foo
 def do_the_thing
 puts "Foo!"
 super if defined? super


    end
 end
 
 class Me
 include Foo
 
 def do_the_thing
 puts "Me!"
 super if defined? super
 end
 end Me.new.do_the_thing
  117. module Foo
 def do_the_thing
 puts "Foo!"
 super if defined? super


    end
 end
 
 class Me
 include Foo
 
 def do_the_thing
 puts "Me!"
 super if defined? super
 end
 end Me.new.do_the_thing "Me!"
 "Foo!"
  118. $ say "it all"

  119. None
  120. Stupid Stupid

  121. None
  122. None
  123. Stupid Ruby Trick #5 DATA and __END__ Stupid Ruby Trick

    #5 DATA and __END__
  124. class Countries
 COUNTRIES = ["Afghanistan", “Aland Islands",
 "Andorra", “Angola", "Anguilla",


    "Antarctica", "Antigua And Barbuda",
 "Argentina", "Armenia", "Aruba",
 "Australia", “Austria", …]
 end
  125. class Countries
 COUNTRIES = DATA.read.split
 end
 
 __END__
 Afghanistan,Aland Islands,Albania,Algeria,…

  126. require "yaml"
 
 class Countries
 COUNTRIES = YAML.load DATA.read
 end


    
 __END__
 ---
 - name: Afghanistan
 code: AF
 - name: "Åland Islands"
 code: AX …
  127. None
  128. None
  129. Stupid Stupid

  130. Stupid Ruby Trick #6 pstore Stupid Ruby Trick #6 pstore

  131. require "pstore"
 
 db = PStore.new("stupid.pstore")
 
 db.transaction do
 db["useful

    info"] = 2
 db["puns"] = 999999999
 end
 
 db.transaction true do
 puts "This session has had #{db["useful info"]} tips."
 puts "So far we have had #{db["puns"]} puns."
 end
  132. # Update the data
 db.transaction do
 db["useful info"] +=1
 db["puns"]

    = Float::INFINITY
 end
 
 db.transaction true do
 puts "This session has had #{db["useful info"]} tips."
 puts "So far we have had #{db["puns"]} puns."
 end
  133. None
  134. require "pstore"
 require "ostruct"
 
 db = PStore.new("stupid.pstore")
 
 local_obj

    = OpenStruct.new name: "Mike Moore",
 username: "blowmage"

  135. require "pstore"
 require "ostruct"
 
 db = PStore.new("stupid.pstore")
 
 local_obj

    = OpenStruct.new name: "Mike Moore",
 username: "blowmage"
 db.transaction do
 db["user"] = local_obj
 end

  136. require "pstore"
 require "ostruct"
 
 db = PStore.new("stupid.pstore")
 
 local_obj

    = OpenStruct.new name: "Mike Moore",
 username: "blowmage"
 db.transaction do
 db["user"] = local_obj
 end
 from_db = nil
 db.transaction do
 from_db = db["user"]
 end
  137. None
  138. class User
 attr_accessor :name, :username
 end

  139. class User
 attr_accessor :name, :username
 end

  140. class User
 attr_accessor :name, :username
 end require "pstore"
 
 db

    = PStore.new("stupid.pstore")

  141. class User
 attr_accessor :name, :username
 end require "pstore"
 
 db

    = PStore.new("stupid.pstore")
 local_obj = User.new.tap do |user|
 user.name = "Mike Moore"
 user.username = "blowmage"
 end

  142. class User
 attr_accessor :name, :username
 end require "pstore"
 
 db

    = PStore.new("stupid.pstore")
 local_obj = User.new.tap do |user|
 user.name = "Mike Moore"
 user.username = "blowmage"
 end
 db.transaction { db["user"] = local_obj }

  143. class User
 attr_accessor :name, :username
 end require "pstore"
 
 db

    = PStore.new("stupid.pstore")
 local_obj = User.new.tap do |user|
 user.name = "Mike Moore"
 user.username = "blowmage"
 end
 db.transaction { db["user"] = local_obj }
 from_db = db.transaction { db["user"] }
  144. require "yaml/store"
 
 class User
 attr_accessor :name, :username
 end
 


    db = YAML::Store.new("stupid.yml")
 
 local_obj = User.new.tap do |user|
 user.name = "Mike Moore"
 user.username = "blowmage"
 end
 
 db.transaction { db["user"] = local_obj }
 
 from_db = db.transaction { db["user"] }
  145. None
  146. None
  147. Stupid Stupid

  148. None
  149. Better Sources • http://confreaks.tv/videos/railsconf2012-ten-things-you-didn-t- know-rails-could-do • http://www.thagomizer.com/blog/2015/02/20/the-cool_shit- file.html

  150. Thank you! Thank you!

  151. Thank you! Thank you!