class RubyLint::Definition::RubyMethod

The RubyMethod definition class is a definition class used for storing information about Ruby methods (both class and instance methods).

@see RubyLint::Definition::RubyObject

@!attribute [r] visibility

@return [Symbol] The method visibility such as `:public`.

@!attribute [r] return_value

@return [Mixed] The value that is returned by the method.

@!attribute [r] calls

@return [Array<RubyLint::MethodCallInfo>] The method calls made in the
 body of this method.

@!attribute [r] callers

@return [Array<RubyLint::MethodCallInfo>] The methods that called this
 method.

Attributes

callers[R]
calls[R]
return_value[R]
visibility[R]

Public Instance Methods

after_initialize() click to toggle source

Called after a new instance of this class is created.

# File lib/ruby-lint/definition/ruby_method.rb, line 29
def after_initialize
  @calls   = []
  @callers = []
end
arguments() click to toggle source

@return [Array]

# File lib/ruby-lint/definition/ruby_method.rb, line 37
def arguments
  return list(:arg)
end
block_argument() click to toggle source

@return [RubyLint::Definition::RubyObject]

# File lib/ruby-lint/definition/ruby_method.rb, line 44
def block_argument
  return list(:blockarg).first
end
define_argument(name) click to toggle source

Defines a required argument for the method.

@example

method.define_argument('number')

@param [String] name The name of the argument.

# File lib/ruby-lint/definition/ruby_method.rb, line 93
def define_argument(name)
  create_argument(:arg, name)
end
define_block_argument(name) click to toggle source

Defines a block argument for the method.

@see RubyLint::Definition::RubyObject#define_argument

# File lib/ruby-lint/definition/ruby_method.rb, line 129
def define_block_argument(name)
  create_argument(:blockarg, name)
end
define_keyword_argument(name) click to toggle source

Defines a keyword argument for the method.

@see RubyLint::Definition::RubyObject#define_argument

# File lib/ruby-lint/definition/ruby_method.rb, line 102
def define_keyword_argument(name)
  create_argument(:kwoptarg, name)
end
define_optional_argument(name) click to toggle source

Defines a optional argument for the method.

@see RubyLint::Definition::RubyObject#define_argument

# File lib/ruby-lint/definition/ruby_method.rb, line 111
def define_optional_argument(name)
  create_argument(:optarg, name)
end
define_rest_argument(name) click to toggle source

Defines a rest argument for the method.

@see RubyLint::Definition::RubyObject#define_argument

# File lib/ruby-lint/definition/ruby_method.rb, line 120
def define_rest_argument(name)
  create_argument(:restarg, name)
end
keyword_arguments() click to toggle source

@return [Array]

# File lib/ruby-lint/definition/ruby_method.rb, line 51
def keyword_arguments
  return list(:kwoptarg)
end
optional_arguments() click to toggle source

@return [Array]

# File lib/ruby-lint/definition/ruby_method.rb, line 58
def optional_arguments
  return list(:optarg)
end
rest_argument() click to toggle source

@return [RubyLint::Definition::RubyObject]

# File lib/ruby-lint/definition/ruby_method.rb, line 65
def rest_argument
  return list(:restarg).first
end
returns(value = nil, &block) click to toggle source

Sets the return value of this method. If a block is given it will be used as the return value. The block is not evaluated until it's called.

@example

string.define_instance_method(:gsub) do |method|
  method.returns('...')
end

@param [Mixed] value

# File lib/ruby-lint/definition/ruby_method.rb, line 81
def returns(value = nil, &block)
  @return_value = block_given? ? block : value
end

Private Instance Methods

create_argument(type, name) click to toggle source

Adds a new argument to the method as well as adding it as a local variable. Note that although the argument's variable is saved under a argument key (e.g. `:arg`) the actual definition type is set to `:lvar`.

@param [Symbol] type The type of argument. @param [String] name The name of the argument.

@return [RubyLint::Definition::RubyObject]

# File lib/ruby-lint/definition/ruby_method.rb, line 146
def create_argument(type, name)
  argument = RubyObject.new(:type => :lvar, :name => name)

  add(argument.type, argument.name, argument)
  add(type, argument.name, argument)

  return argument
end