Slide 1

Slide 1 text

Stupid Ruby Tricks Stupid Ruby Tricks Mike Moore @blowmage Mike Moore @blowmage

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Stupid Stupid

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

Mike Moore Mike Moore

Slide 7

Slide 7 text

Mike Moore Mike Moore @blowmage @blowmage

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

Stupid Stupid

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

Its full of puns! Its full of puns!

Slide 30

Slide 30 text

Stupid Stupid

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

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


Slide 34

Slide 34 text

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)

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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


Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

Stupid Stupid

Slide 41

Slide 41 text

Stupid Ruby Trick #2 Optional Parenthesis Stupid Ruby Trick #2 Optional Parenthesis

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

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"

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

Stupid Stupid

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

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

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

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

Slide 54

Slide 54 text

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

Slide 55

Slide 55 text

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

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

hash_list = Hash.new []

Slide 58

Slide 58 text

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

Slide 59

Slide 59 text

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

Slide 60

Slide 60 text

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

Slide 61

Slide 61 text

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

Slide 62

Slide 62 text

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]

Slide 63

Slide 63 text

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]

Slide 64

Slide 64 text

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]

Slide 65

Slide 65 text

No content

Slide 66

Slide 66 text

default #=> "default_value"
 h = Hash.new default


Slide 67

Slide 67 text

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


Slide 68

Slide 68 text

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


Slide 69

Slide 69 text

default #=> "default_value"
 h = Hash.new default
 h[:nope] #=> "default_value"
 default.upcase!
 h[:nope] #=> "DEFAULT_VALUE"

Slide 70

Slide 70 text

No content

Slide 71

Slide 71 text

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

Slide 72

Slide 72 text

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

Slide 73

Slide 73 text

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

Slide 74

Slide 74 text

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]

Slide 75

Slide 75 text

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

Slide 76

Slide 76 text

No content

Slide 77

Slide 77 text

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

Slide 78

Slide 78 text

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

Slide 79

Slide 79 text

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

Slide 80

Slide 80 text

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

Slide 81

Slide 81 text

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

Slide 82

Slide 82 text

No content

Slide 83

Slide 83 text

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

Slide 84

Slide 84 text

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

Slide 85

Slide 85 text

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

Slide 86

Slide 86 text

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

Slide 87

Slide 87 text

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

Slide 88

Slide 88 text

No content

Slide 89

Slide 89 text

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

Slide 90

Slide 90 text

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

Slide 91

Slide 91 text

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

Slide 92

Slide 92 text

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

Slide 93

Slide 93 text

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

Slide 94

Slide 94 text

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

Slide 95

Slide 95 text

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

Slide 96

Slide 96 text

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

Slide 97

Slide 97 text

No content

Slide 98

Slide 98 text

Stupid Stupid

Slide 99

Slide 99 text

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

Slide 100

Slide 100 text

module Foo
 def do_the_thing
 puts "Foo!"
 end
 end
 class Me
 include Foo
 
 def do_the_thing
 puts "Me!"
 end
 end


Slide 101

Slide 101 text

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

Slide 102

Slide 102 text

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

Slide 103

Slide 103 text

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

Slide 104

Slide 104 text

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

Slide 105

Slide 105 text

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

Slide 106

Slide 106 text

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

Slide 107

Slide 107 text

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

Slide 108

Slide 108 text

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

Slide 109

Slide 109 text

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

Slide 110

Slide 110 text

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

Slide 111

Slide 111 text

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 #

Slide 112

Slide 112 text

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

Slide 113

Slide 113 text

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

Slide 114

Slide 114 text

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

Slide 115

Slide 115 text

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

Slide 116

Slide 116 text

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

Slide 117

Slide 117 text

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

Slide 118

Slide 118 text

$ say "it all"

Slide 119

Slide 119 text

No content

Slide 120

Slide 120 text

Stupid Stupid

Slide 121

Slide 121 text

No content

Slide 122

Slide 122 text

No content

Slide 123

Slide 123 text

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

Slide 124

Slide 124 text

class Countries
 COUNTRIES = ["Afghanistan", “Aland Islands",
 "Andorra", “Angola", "Anguilla",
 "Antarctica", "Antigua And Barbuda",
 "Argentina", "Armenia", "Aruba",
 "Australia", “Austria", …]
 end

Slide 125

Slide 125 text

class Countries
 COUNTRIES = DATA.read.split
 end
 
 __END__
 Afghanistan,Aland Islands,Albania,Algeria,…

Slide 126

Slide 126 text

require "yaml"
 
 class Countries
 COUNTRIES = YAML.load DATA.read
 end
 
 __END__
 ---
 - name: Afghanistan
 code: AF
 - name: "Åland Islands"
 code: AX …

Slide 127

Slide 127 text

No content

Slide 128

Slide 128 text

No content

Slide 129

Slide 129 text

Stupid Stupid

Slide 130

Slide 130 text

Stupid Ruby Trick #6 pstore Stupid Ruby Trick #6 pstore

Slide 131

Slide 131 text

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

Slide 132

Slide 132 text

# 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

Slide 133

Slide 133 text

No content

Slide 134

Slide 134 text

require "pstore"
 require "ostruct"
 
 db = PStore.new("stupid.pstore")
 
 local_obj = OpenStruct.new name: "Mike Moore",
 username: "blowmage"


Slide 135

Slide 135 text

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


Slide 136

Slide 136 text

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

Slide 137

Slide 137 text

No content

Slide 138

Slide 138 text

class User
 attr_accessor :name, :username
 end

Slide 139

Slide 139 text

class User
 attr_accessor :name, :username
 end

Slide 140

Slide 140 text

class User
 attr_accessor :name, :username
 end require "pstore"
 
 db = PStore.new("stupid.pstore")


Slide 141

Slide 141 text

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


Slide 142

Slide 142 text

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 }


Slide 143

Slide 143 text

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

Slide 144

Slide 144 text

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

Slide 145

Slide 145 text

No content

Slide 146

Slide 146 text

No content

Slide 147

Slide 147 text

Stupid Stupid

Slide 148

Slide 148 text

No content

Slide 149

Slide 149 text

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

Slide 150

Slide 150 text

Thank you! Thank you!

Slide 151

Slide 151 text

Thank you! Thank you!