Why can I pass an Exception object to the initialize super argument in Ruby's Exception wrapper?

Asked 2 years ago, Updated 2 years ago, 40 views

I found the following when I tried to wrapper Ruby's Exception

https://gist.github.com/rks/2577339#gistcomment-1313963

Quote

class ClientError <StandardError
  def initialize(e=nil)
    super
    # Preserve the original exception's data if provided
    if & e.is_a? (Exception)
      set_backtrace e.backtrace
      message.prepend"#{e.class}:"
    end
  end
end

http://ruby-doc.org/core-2.2.0/Exception.html
I checked and found that

Quote:

new(msg=nil) → exception click to toggle source
Construct a new Exception object, optional passing in a message.

It says, and it doesn't even mention the exact type, but I think I'm expecting a string.

However, when I actually tried the above quotation source, I think it worked fine.
Is super the correct source?

ruby

2022-09-30 15:48

1 Answers

I'm not expecting a string, but anything that implements to_s is fine.

If you pass the number 1, it will still be displayed.

[8]pry(main)>e2=ArgumentError.new(1)
=>#<ArgumentError:1>
[9] pry(main)>e2.message
= > "1"


2022-09-30 15:48

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.