Skip to content

Commit 192c42a

Browse files
authoredJan 27, 2024
Merge pull request #157 from buty4649/improve-non-existent-method
Change non-existent method behavior to NoMethodError for clarity
2 parents 7eb6a37 + f9116e7 commit 192c42a

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed
 

‎mrblib/rf/container.rb

+13-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
module Rf
44
class Container
5+
class NoMethodError < StandardError
6+
def initialize(sym)
7+
super("undefined method `#{sym}'. Record is an instance of #{Record.class}}")
8+
end
9+
end
10+
511
attr_accessor :filename
612

713
def initialize(opts = {})
@@ -60,21 +66,19 @@ def generate_line_prefix
6066
end.join
6167
end
6268

63-
%i[gsub gsub! sub sub! tr tr!].each do |sym|
69+
%i[grep grep_v gsub gsub! sub sub!].each do |sym|
6470
define_method(sym) do |*args, &block|
65-
_.__send__(sym, *args, &block) if string?
71+
raise NoMethodError, sym unless _.respond_to?(sym)
72+
73+
_.__send__(sym, *args, &block)
6674
end
6775
end
6876

69-
%i[dig].each do |sym|
77+
%i[dig tr tr!].each do |sym|
7078
define_method(sym) do |*args|
71-
_.__send__(sym, *args) if hash?
72-
end
73-
end
79+
raise NoMethodError, sym unless _.respond_to?(sym)
7480

75-
%i[grep grep_v].each do |sym|
76-
define_method(sym) do |*args, &block|
77-
_.__send__(sym, *args, &block) if array?
81+
_.__send__(sym, *args)
7882
end
7983
end
8084

0 commit comments

Comments
 (0)
Please sign in to comment.