Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Cleaned up code for retrieving method parameters.

The use of Array#map would require the use of Array#compact to get rid of
NilClass values in the parameters collection. An easier (and probably a bit
faster way) of doing this is to simply use Array#each and push values into
another Array.

Signed-off-by: Yorick Peterse <yorickpeterse@gmail.com>
  • Loading branch information...
commit c8a2535ca5ca8d0c7e5784ed35847559bf0ae1ce 1 parent fdd376e
@YorickPeterse YorickPeterse authored
Showing with 12 additions and 29 deletions.
  1. +11 −7 kernel/common/compiled_code.rb
  2. +1 −22 kernel/common/method.rb
View
18 kernel/common/compiled_code.rb
@@ -510,26 +510,30 @@ def get_metadata(key)
##
# For Method#parameters
def parameters
+ params = []
+
+ return params unless respond_to?(:local_names)
+
m = required_args - post_args
o = m + total_args - required_args
p = o + post_args
p += 1 if splat
- params = local_names.each_with_index.map do |name, i|
+ local_names.each_with_index do |name, i|
if i < m
- [:req, name]
+ params << [:req, name]
elsif i < o
- [:opt, name]
+ params << [:opt, name]
elsif splat == i
- [:rest, name]
+ params << [:rest, name]
elsif i < p
- [:req, name]
+ params << [:req, name]
elsif block == i
- [:block, name]
+ params << [:block, name]
end
end
- return params.compact
+ return params
end
##
View
23 kernel/common/method.rb
@@ -262,28 +262,7 @@ def source_location
end
def parameters
- return [] unless @executable.respond_to? :local_names
-
- m = @executable.required_args - @executable.post_args
- o = m + @executable.total_args - @executable.required_args
- p = o + @executable.post_args
- p += 1 if @executable.splat
-
- params = @executable.local_names.each_with_index.map do |name, i|
- if i < m
- [:req, name]
- elsif i < o
- [:opt, name]
- elsif @executable.splat == i
- [:rest, name]
- elsif i < p
- [:req, name]
- elsif @executable.block == i
- [:block, name]
- end
- end
-
- return params.compact
+ return @executable.respond_to?(:parameters) ? @executable.parameters : []
end
def owner
Please sign in to comment.
Something went wrong with that request. Please try again.