Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Stupid Ruby Tricks

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on.
Avatar for blowmage 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.

Avatar for blowmage

blowmage

March 07, 2015
Tweet

More Decks by blowmage

Other Decks in Technology

Transcript

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

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

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

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

    in time. No problem. We’ll move Terence Lee to Thursday. Sweet!
  5. 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?!?
  6. 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?!?
  7. 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?!?
  8. 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)
  9. # File.open("stupid.txt", "w") do |file|
 # file.write("Its all about closures,

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

  10. # 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)
  11. # 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"))
  12. 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"))
  13. 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"))
  14. 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"
  15. h = { foo: "FOO", bar: "BAR" } h[:baz] #=>

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

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

    nil h.fetch :baz, "BAZ" #=> “BAZ" h[:baz] = "TROLLFACE" h.fetch :baz, "BAZ" #=> "TROLLFACE"
  18. 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]
  19. 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]
  20. 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]
  21. hash_list = Hash.new { |h, k| h[k] = [] }

    hash_list[:nope] hash_list[:nope] << :x
 hash_list[:nope] << :y
 hash_list[:nope] << :z
  22. 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]
  23. 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] #=> []
  24. hash_recurs = Hash.new { |h, k| h[k] = {} }

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

    hash_recurs #=> {} hash_recurs[:x] #=> {} hash_recurs[:x][:y] #=> nil
  26. 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
  27. hash_recurs = Hash.new do |h, k|
 h[k] = Hash.new &h.default_proc


    end hash_recurs[:x] #=> {} hash_recurs[:x][:y] #=> {}
  28. 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] #=> {}
  29. 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=>{}}}}
  30. frankenstein = Hash.new { |h, k| h[k] = [] }

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

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

    frankenstein[:nope] << :x << :y << :z frankenstein[:nope] #=> [:x, :y, :z] frankenstein[:not_even] #=> []
  33. 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=>[]}
  34. 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
  35. 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] #=> {}
  36. 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=>{}}}}
  37. Stupid Ruby Trick #4 include vs. prepend and super Stupid

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


    include Foo
 
 def do_the_thing
 puts "Me!"
 end
 end

  39. 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
  40. 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!"
  41. module Foo
 def do_the_thing
 puts "Foo!"
 end
 end
 class Me


    include Foo
 
 def do_the_thing
 puts "Me!"
 super
 end
 end
  42. 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
  43. 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!"
  44. module Foo
 def do_the_thing
 puts "Foo!"
 end
 end
 
 class

    Me
 prepend Foo
 
 def do_the_thing
 puts "Me!"
 super
 end
 end
  45. 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
  46. 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!"
  47. module Foo
 def do_the_thing
 puts "Foo!"
 super
 end
 end
 


    class Me
 prepend Foo
 
 def do_the_thing
 puts "Me!"
 super
 end
 end
  48. 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
  49. 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>
  50. 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
  51. 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
  52. 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!"
  53. 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
  54. 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
  55. 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!"
  56. class Countries
 COUNTRIES = ["Afghanistan", “Aland Islands",
 "Andorra", “Angola", "Anguilla",


    "Antarctica", "Antigua And Barbuda",
 "Argentina", "Armenia", "Aruba",
 "Australia", “Austria", …]
 end
  57. require "yaml"
 
 class Countries
 COUNTRIES = YAML.load DATA.read
 end


    
 __END__
 ---
 - name: Afghanistan
 code: AF
 - name: "Åland Islands"
 code: AX …
  58. 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
  59. # 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
  60. require "pstore"
 require "ostruct"
 
 db = PStore.new("stupid.pstore")
 
 local_obj

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

  61. 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

  62. 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
  63. 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

  64. 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 }

  65. 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"] }
  66. 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"] }