Skip to content

Instantly share code, notes, and snippets.

@sgnn7
Created October 15, 2018 17:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sgnn7/46dbfbb4bb6d63f527a2b778ddb9315a to your computer and use it in GitHub Desktop.
Save sgnn7/46dbfbb4bb6d63f527a2b778ddb9315a to your computer and use it in GitHub Desktop.
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