Skip to content

Instantly share code, notes, and snippets.

View denisdefreyne's full-sized avatar

Denis Defreyne denisdefreyne

View GitHub Profile
case 14
when 10..15
puts "Kinda small!"
when 80..99
puts "Kinda large!"
end
10..15 === 14 # => true
80..99 === 14 # => false
point.x = 10
p point.hash # => 910895
p collection.key?(point) # => false
point = Point.new(5, 6)
collection = {}
collection[point] = 42
p point.hash # => 421522
p collection.key?(point) # => true
require "set"
home = Path.new("home", "denis")
also_home = Path.new("home", "denis")
elsewhere = Path.new("usr", "bin")
paths = Set.new
paths << home
paths.include?(home) # => true
require "set"
points = Set.new
points << Point.new(11, 24)
points.include?(Point.new(11, 24)) # => true
points.include?(Point.new(10, 22)) # => false
points = {}
points[Point.new(11, 24)] = true
points[Point.new(11, 24)] # => true
points[Point.new(10, 22)] # => nil
class Point
# …
def hash
[self.class, @x, @y].hash
end
end
class Point
# …
def hash
# Do not do this!
self.class.hash ^ (@x.hash * 3) ^ (@y.hash * 5)
end
end
class Point
# …
def hash
# Do not do this!
self.class.hash ^ @x.hash ^ @y.hash
end
end