Skip to content

Commit

Permalink
Cleaned up code for retrieving method parameters.
Browse files Browse the repository at this point in the history
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
Yorick Peterse committed Mar 24, 2013
1 parent fdd376e commit c8a2535
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 29 deletions.
18 changes: 11 additions & 7 deletions kernel/common/compiled_code.rb
Expand Up @@ -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

##
Expand Down
23 changes: 1 addition & 22 deletions kernel/common/method.rb
Expand Up @@ -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
Expand Down

0 comments on commit c8a2535

Please sign in to comment.