Created
December 15, 2019 15:10
-
-
Save antonvolkoff/5b44b323826368847909082ba76a7f90 to your computer and use it in GitHub Desktop.
You can freeze classes in ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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