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

Stupid Ruby Tricks

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.

blowmage

March 07, 2015
Tweet

More Decks by blowmage

Other Decks in Technology

Transcript

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

    View Slide

  2. View Slide

  3. View Slide

  4. Stupid
    Stupid

    View Slide

  5. View Slide

  6. Mike Moore
    Mike Moore

    View Slide

  7. Mike Moore
    Mike Moore
    @blowmage
    @blowmage

    View Slide

  8. View Slide

  9. View Slide

  10. View Slide

  11. View Slide

  12. View Slide

  13. View Slide

  14. View Slide

  15. View Slide

  16. View Slide

  17. View Slide

  18. View Slide

  19. Stupid
    Stupid

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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?!?

    View Slide

  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?!?

    View Slide

  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?!?

    View Slide

  27. View Slide

  28. View Slide

  29. Its full of puns!
    Its full of puns!

    View Slide

  30. Stupid
    Stupid

    View Slide

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

    View Slide

  32. View Slide

  33. File.open("stupid.txt", "w") do |file|

    file.write("Its all about closures, yo!")

    end


    View Slide

  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)

    View Slide

  35. View Slide

  36. # File.open("stupid.txt", "w") do |file|

    # file.write("Its all about closures, yo!")

    # end

    View Slide

  37. # File.open("stupid.txt", "w") do |file|

    # file.write("Its all about closures, yo!")

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


    View Slide

  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)

    View Slide

  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"))

    View Slide

  40. Stupid
    Stupid

    View Slide

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

    View Slide

  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"))

    View Slide

  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"))

    View Slide

  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"

    View Slide

  45. Stupid Ruby Advice #1
    Don’t overload lines
    Stupid Ruby Advice #1
    Don’t overload lines

    View Slide

  46. View Slide

  47. View Slide

  48. Stupid
    Stupid

    View Slide

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

    View Slide

  50. View Slide

  51. h = { foo: "FOO", bar: "BAR" }

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  56. View Slide

  57. hash_list = Hash.new []

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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]

    View Slide

  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]

    View Slide

  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]

    View Slide

  65. View Slide

  66. default #=> "default_value"

    h = Hash.new default


    View Slide

  67. default #=> "default_value"

    h = Hash.new default

    h[:nope] #=> "default_value"


    View Slide

  68. default #=> "default_value"

    h = Hash.new default

    h[:nope] #=> "default_value"

    default.upcase!


    View Slide

  69. default #=> "default_value"

    h = Hash.new default

    h[:nope] #=> "default_value"

    default.upcase!

    h[:nope] #=> "DEFAULT_VALUE"

    View Slide

  70. View Slide

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

    View Slide

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

    View Slide

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

    hash_list[:nope] << :y

    hash_list[:nope] << :z

    View Slide

  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]

    View Slide

  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] #=> []

    View Slide

  76. View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  82. View Slide

  83. hash_recurs = Hash.new do |h, k|

    h[k] = Hash.new &h.default_proc

    end

    View Slide

  84. hash_recurs = Hash.new do |h, k|

    h[k] = Hash.new &h.default_proc

    end
    hash_recurs[:x] #=> {}

    View Slide

  85. hash_recurs = Hash.new do |h, k|

    h[k] = Hash.new &h.default_proc

    end
    hash_recurs[:x] #=> {}
    hash_recurs[:x][:y] #=> {}

    View Slide

  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] #=> {}

    View Slide

  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=>{}}}}

    View Slide

  88. View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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=>[]}

    View Slide

  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

    View Slide

  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] #=> {}

    View Slide

  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=>{}}}}

    View Slide

  97. View Slide

  98. Stupid
    Stupid

    View Slide

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

    View Slide

  100. module Foo

    def do_the_thing

    puts "Foo!"

    end

    end

    class Me

    include Foo


    def do_the_thing

    puts "Me!"

    end

    end


    View Slide

  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

    View Slide

  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!"

    View Slide

  103. module Foo

    def do_the_thing

    puts "Foo!"

    end

    end

    class Me

    include Foo


    def do_the_thing

    puts "Me!"

    super

    end

    end

    View Slide

  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

    View Slide

  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!"

    View Slide

  106. module Foo

    def do_the_thing

    puts "Foo!"

    end

    end


    class Me

    prepend Foo


    def do_the_thing

    puts "Me!"

    super

    end

    end

    View Slide

  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

    View Slide

  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!"

    View Slide

  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

    View Slide

  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

    View Slide

  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 #

    View Slide

  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

    View Slide

  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

    View Slide

  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!"

    View Slide

  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

    View Slide

  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

    View Slide

  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!"

    View Slide

  118. $ say "it all"

    View Slide

  119. View Slide

  120. Stupid
    Stupid

    View Slide

  121. View Slide

  122. View Slide

  123. Stupid Ruby Trick #5
    DATA and __END__
    Stupid Ruby Trick #5
    DATA and __END__

    View Slide

  124. class Countries

    COUNTRIES = ["Afghanistan", “Aland Islands",

    "Andorra", “Angola", "Anguilla",

    "Antarctica", "Antigua And Barbuda",

    "Argentina", "Armenia", "Aruba",

    "Australia", “Austria", …]

    end

    View Slide

  125. class Countries

    COUNTRIES = DATA.read.split

    end


    __END__

    Afghanistan,Aland Islands,Albania,Algeria,…

    View Slide

  126. require "yaml"


    class Countries

    COUNTRIES = YAML.load DATA.read

    end


    __END__

    ---

    - name: Afghanistan

    code: AF

    - name: "Åland Islands"

    code: AX …

    View Slide

  127. View Slide

  128. View Slide

  129. Stupid
    Stupid

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

  133. View Slide

  134. require "pstore"

    require "ostruct"


    db = PStore.new("stupid.pstore")


    local_obj = OpenStruct.new name: "Mike Moore",

    username: "blowmage"


    View Slide

  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


    View Slide

  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

    View Slide

  137. View Slide

  138. class User

    attr_accessor :name, :username

    end

    View Slide

  139. class User

    attr_accessor :name, :username

    end

    View Slide

  140. class User

    attr_accessor :name, :username

    end
    require "pstore"


    db = PStore.new("stupid.pstore")


    View Slide

  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


    View Slide

  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 }


    View Slide

  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"] }

    View Slide

  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"] }

    View Slide

  145. View Slide

  146. View Slide

  147. Stupid
    Stupid

    View Slide

  148. View Slide

  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

    View Slide

  150. Thank you!
    Thank you!

    View Slide

  151. Thank you!
    Thank you!

    View Slide