Skip to content

Instantly share code, notes, and snippets.

@antonvolkoff
Created December 15, 2019 15:10
Show Gist options
  • Save antonvolkoff/5b44b323826368847909082ba76a7f90 to your computer and use it in GitHub Desktop.
Save antonvolkoff/5b44b323826368847909082ba76a7f90 to your computer and use it in GitHub Desktop.
You can freeze classes in ruby
class Point
attr_reader :x, :y
def initialize(x, y)
@x = x
@y = y
freeze
end
def move!(x_offset, y_offset)
@x = x + x_offset
@y = y + y_offset
end
def move(x_offset, y_offset)
Point.new(x + x_offset, y + y_offset)
end
end
p = Point.new(10, 10)
p.move(-5, -5)
p = Point.new(5, 5)
p.move!(5, 5)
# => can't modify frozen Point (FrozenError)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment