Java で記述されたコードを 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 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