문제

Rather than have my Ruby object's constructor complain about the number of arguments given (in the event of bad args), I'd like the message to list what was expected, exactly.

def initialize a, b, c
  begin
    @a = a
    @b = b
    @c = c
  rescue ArgumentError
    raise ArgumentError, "my custom error message"
  end
end

Obviously this won't work, but hopefully this gets the idea across. I just want to override the default ArgumentError message for this class.

도움이 되었습니까?

해결책

def initialze(*args)
  raise ArgumentError, 'my message' unless valid_according_to_my_rules(args)

  @a, @b, @c = args
end
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top