Created
October 15, 2018 17:47
-
-
Save sgnn7/46dbfbb4bb6d63f527a2b778ddb9315a to your computer and use it in GitHub Desktop.
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 OldErrorClass | |
def self.new(msg) | |
Class.new(RuntimeError) do | |
def initialize(*args) | |
@args = args | |
end | |
define_method(:to_s) do | |
@args.each.with_index.reduce(msg) do |m,(x,i)| | |
m.gsub(Regexp.new("\\{#{i}}"), x || 'nil') | |
end | |
end | |
end | |
end | |
end | |
class NewErrorClass | |
def self.new(msg) | |
Class.new(RuntimeError) do | |
def initialize(*args) | |
@args = args | |
end | |
define_method(:to_s) do | |
@args.each_with_index.reduce(msg) do |m, (x, arg_index)| | |
x_stringified = x.nil? ? 'nil' : x.to_s | |
m.gsub(Regexp.new("\\{#{arg_index}}"), x_stringified) | |
end | |
end | |
end | |
end | |
end | |
puts NewErrorClass.new("\#{0} / \#{1} / \#{2} / \#{3} / \#{4} / \#{5}").new(true, false, nil, "abc", "", ["A", "B"]) | |
puts OldErrorClass.new("\#{0} / \#{1} / \#{2} / \#{3} / \#{4} / \#{5}").new("abc", "", false, nil) | |
puts OldErrorClass.new("\#{0} / \#{1} / \#{2} / \#{3} / \#{4} / \#{5}").new(true, false, nil, "abc", "", ["A", "B"]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment