Skip to content

Instantly share code, notes, and snippets.

@gouf
Last active May 4, 2022
Embed
What would you like to do?
Java で記述されたコードを Ruby で再現
class GiftPoint
attr_reader :value
MIN_POINT = 0
STANDARD_MEMBERSHIP_POINT = 3_000
PREMIUM_MEMBERSHIP_POINT = 10_000
def initialize(point)
if point < MIN_POINT
raise RuntimeError, "ポイントが0以上ではありません"
end
@value = point
end
private_class_method :new
private_constant :MIN_POINT, :STANDARD_MEMBERSHIP_POINT, :PREMIUM_MEMBERSHIP_POINT
class << self
def for_standard_membership
new(STANDARD_MEMBERSHIP_POINT)
end
def for_premium_membership
new(PREMIUM_MEMBERSHIP_POINT)
end
end
end
standard_membership_point = GiftPoint.for_standard_membership
premium_membership_point = GiftPoint.for_premium_membership
p standard_membership_point.value # => 3000
p premium_membership_point.value # => 10000
# It gets error:
gift_point = GiftPoint.new(100)
# => private method `new' called for GiftPoint:Class (NoMethodError)`
p gift_point.value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment