Module: GetArgs

Included in:
Method, UnboundMethod
Defined in:
lib/mri_args.rb,
lib/vm_args.rb,
lib/get_args.rb,
lib/jruby_args.rb,
lib/argument_list.rb

Overview

Used in mapping controller arguments to the params hash. NOTE: You must have the ‘ruby2ruby’ gem installed for this to work.

Examples

# In PostsController
def show(id)  #=> id is the same as params[:id]

Defined Under Namespace

Classes: Argument, ArgumentList

Constant Summary collapse

Methods =
org.jruby.internal.runtime.methods

Instance Method Summary collapse

Instance Method Details

#build_args(args_node) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/jruby_args.rb', line 25

def build_args(args_node)
  args = []
  required = []
  optional = []

  # required args
  if (args_node.args && args_node.args.size > 0)
    required << args_node.args.child_nodes.map { |arg| [arg.name.to_s.intern] }
  end
  
  # optional args
  if (args_node.opt_args && args_node.opt_args.size > 0)
    optional << args_node.opt_args.child_nodes.map do |arg|
      name = arg.name.to_s.intern
      value_node = arg.value_node
      case value_node
      when org.jruby.ast::FixnumNode
        value = value_node.value
      when org.jruby.ast::SymbolNode
        value = value_node.get_symbol(JRuby.runtime)
      when org.jruby.ast::StrNode
        value = value_node.value
      else
        value = nil
      end
      [name, value]
    end
  end

  first_args = required.first
  optional.first.each {|arg| first_args << arg} if optional.first
  
  args = [first_args]
  
  rest = args_node.rest_arg_node
  args << (rest ? rest.name.to_s.intern : nil)
  
  block = args_node.block_arg_node
  args << (block ? block.name.to_s.intern : nil)

  args
end

#get_argsObject

Returns

Array

Method arguments and their default values.

Examples

class Example
  def hello(one,two="two",three)
  end

  def goodbye
  end
end

Example.instance_method(:hello).get_args
  #=> [[:one], [:two, "two"], [:three, "three"]]
Example.instance_method(:goodbye).get_args  #=> nil


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/mri_args.rb', line 82

def get_args
  unless respond_to?(:parameters)
    raise NotImplementedError, "Ruby #{RUBY_VERSION} doesn't support #{self.class}#parameters"
  end

  required = []
  optional = []

  parameters.each do |(type, name)|
    if type == :opt
      required << [name, nil]
      optional << name
    else
      required << [name]
    end
  end

  return [required, optional]
end

#get_argument_listObject



15
16
17
# File 'lib/get_args.rb', line 15

def get_argument_list
  ArgumentList.new(get_args.first)
end