Results for: "module_function"

No documentation available

Returns the Ruby source filename and line number containing the definition of the constant specified. If the named constant is not found, nil is returned. If the constant is found, but its source location can not be extracted (constant is defined in C code), empty array is returned.

inherit specifies whether to lookup in mod.ancestors (true by default).

# test.rb:
class A         # line 1
  C1 = 1
  C2 = 2
end

module M        # line 6
  C3 = 3
end

class B < A     # line 10
  include M
  C4 = 4
end

class A # continuation of A definition
  C2 = 8 # constant redefinition; warned yet allowed
end

p B.const_source_location('C4')           # => ["test.rb", 12]
p B.const_source_location('C3')           # => ["test.rb", 7]
p B.const_source_location('C1')           # => ["test.rb", 2]

p B.const_source_location('C3', false)    # => nil  -- don't lookup in ancestors

p A.const_source_location('C2')           # => ["test.rb", 16] -- actual (last) definition place

p Object.const_source_location('B')       # => ["test.rb", 10] -- top-level constant could be looked through Object
p Object.const_source_location('A')       # => ["test.rb", 1] -- class reopening is NOT considered new definition

p B.const_source_location('A')            # => ["test.rb", 1]  -- because Object is in ancestors
p M.const_source_location('A')            # => ["test.rb", 1]  -- Object is not ancestor, but additionally checked for modules

p Object.const_source_location('A::C1')   # => ["test.rb", 2]  -- nesting is supported
p Object.const_source_location('String')  # => []  -- constant is defined in C code

Removes the named class variable from the receiver, returning that variable’s value.

class Example
  @@var = 99
  puts remove_class_variable(:@@var)
  p(defined? @@var)
end

produces:

99
nil

When this module is included in another, Ruby calls append_features in this module, passing it the receiving module in mod. Ruby’s default implementation is to add the constants, methods, and module variables of this module to mod if this module has not already been added to mod or one of its ancestors. See also Module#include.

When this module is prepended in another, Ruby calls prepend_features in this module, passing it the receiving module in mod. Ruby’s default implementation is to overlay the constants, methods, and module variables of this module to mod if this module has not already been added to mod or one of its ancestors. See also Module#prepend.

Returns an array of all modules used in the current scope. The ordering of modules in the resulting array is not defined.

module A
  refine Object do
  end
end

module B
  refine Object do
  end
end

using A
using B
p Module.used_refinements

produces:

[#<refinement:Object@B>, #<refinement:Object@A>]

Returns a string representing this module or class. For basic classes and modules, this is the name. For singletons, we show information on the thing we’re attached to as well.

Creates instance variables and corresponding methods that return the value of each instance variable. Equivalent to calling “attr:name” on each name in turn. String arguments are converted to symbols. Returns an array of defined method names as symbols.

Creates an accessor method to allow assignment to the attribute symbol.id2name. String arguments are converted to symbols. Returns an array of defined method names as symbols.

Defines a named attribute for this module, where the name is symbol.id2name, creating an instance variable (@name) and a corresponding access method to read it. Also creates a method called name= to set the attribute. String arguments are converted to symbols. Returns an array of defined method names as symbols.

module Mod
  attr_accessor(:one, :two) #=> [:one, :one=, :two, :two=]
end
Mod.instance_methods.sort   #=> [:one, :one=, :two, :two=]

Evaluates the given block in the context of the class/module. The method defined in the block will belong to the receiver. Any arguments passed to the method will be passed to the block. This can be used if the block needs to access instance variables.

class Thing
end
Thing.class_exec{
  def hello() "Hello there!" end
}
puts Thing.new.hello()

produces:

Hello there!

Evaluates the string or block in the context of mod, except that when a block is given, constant/class variable lookup is not affected. This can be used to add methods to a class. module_eval returns the result of evaluating its argument. The optional filename and lineno parameters set the text for error messages.

class Thing
end
a = %q{def hello() "Hello there!" end}
Thing.module_eval(a)
puts Thing.new.hello()
Thing.module_eval("invalid code", "dummy", 123)

produces:

Hello there!
dummy:123:in `module_eval': undefined local variable
    or method `code' for Thing:Class

For the given method names, marks the method as passing keywords through a normal argument splat. This should only be called on methods that accept an argument splat (*args) but not explicit keywords or a keyword splat. It marks the method such that if the method is called with keyword arguments, the final hash argument is marked with a special flag such that if it is the final element of a normal argument splat to another method call, and that method call does not include explicit keywords or a keyword splat, the final element is interpreted as keywords. In other words, keywords will be passed through the method to other methods.

This should only be used for methods that delegate keywords to another method, and only for backwards compatibility with Ruby versions before 3.0. See www.ruby-lang.org/en/news/2019/12/12/separation-of-positional-and-keyword-arguments-in-ruby-3-0/ for details on why ruby2_keywords exists and when and how to use it.

This method will probably be removed at some point, as it exists only for backwards compatibility. As it does not exist in Ruby versions before 2.7, check that the module responds to this method before calling it:

module Mod
  def foo(meth, *args, &block)
    send(:"do_#{meth}", *args, &block)
  end
  ruby2_keywords(:foo) if respond_to?(:ruby2_keywords, true)
end

However, be aware that if the ruby2_keywords method is removed, the behavior of the foo method using the above approach will change so that the method does not pass through keywords.

Returns the value of the given class variable (or throws a NameError exception). The @@ part of the variable name should be included for regular class variables. String arguments are converted to symbols.

class Fred
  @@foo = 99
end
Fred.class_variable_get(:@@foo)     #=> 99

Sets the class variable named by symbol to the given object. If the class variable name is passed as a string, that string is converted to a symbol.

class Fred
  @@foo = 99
  def foo
    @@foo
  end
end
Fred.class_variable_set(:@@foo, 101)     #=> 101
Fred.new.foo                             #=> 101

Returns true if the given class variable is defined in obj. String arguments are converted to symbols.

class Fred
  @@foo = 99
end
Fred.class_variable_defined?(:@@foo)    #=> true
Fred.class_variable_defined?(:@@bar)    #=> false

Returns true if the named public method is defined by mod. If inherit is set, the lookup will also search mod’s ancestors. String arguments are converted to symbols.

module A
  def method1()  end
end
class B
  protected
  def method2()  end
end
class C < B
  include A
  def method3()  end
end

A.method_defined? :method1                 #=> true
C.public_method_defined? "method1"         #=> true
C.public_method_defined? "method1", true   #=> true
C.public_method_defined? "method1", false  #=> true
C.public_method_defined? "method2"         #=> false
C.method_defined? "method2"                #=> true

Returns true if the named private method is defined by mod. If inherit is set, the lookup will also search mod’s ancestors. String arguments are converted to symbols.

module A
  def method1()  end
end
class B
  private
  def method2()  end
end
class C < B
  include A
  def method3()  end
end

A.method_defined? :method1                   #=> true
C.private_method_defined? "method1"          #=> false
C.private_method_defined? "method2"          #=> true
C.private_method_defined? "method2", true    #=> true
C.private_method_defined? "method2", false   #=> false
C.method_defined? "method2"                  #=> false

Makes a list of existing class methods public.

String arguments are converted to symbols. An Array of Symbols and/or Strings is also accepted.

Makes existing class methods private. Often used to hide the default constructor new.

String arguments are converted to symbols. An Array of Symbols and/or Strings is also accepted.

class SimpleSingleton  # Not thread safe
  private_class_method :new
  def SimpleSingleton.create(*args, &block)
    @me = new(*args, &block) if ! @me
    @me
  end
end

module Foo; end ^^^^^^^^^^^^^^^

Sets the temporary name of the module. This name is reflected in introspection of the module and the values that are related to it, such as instances, constants, and methods.

The name should be nil or a non-empty string that is not a valid constant path (to avoid confusing between permanent and temporary names).

The method can be useful to distinguish dynamically generated classes and modules without assigning them to constants.

If the module is given a permanent name by assigning it to a constant, the temporary name is discarded. A temporary name can’t be assigned to modules that have a permanent name.

If the given name is nil, the module becomes anonymous again.

Example:

m = Module.new # => #<Module:0x0000000102c68f38>
m.name #=> nil

m.set_temporary_name("fake_name") # => fake_name
m.name #=> "fake_name"

m.set_temporary_name(nil) # => #<Module:0x0000000102c68f38>
m.name #=> nil

c = Class.new
c.set_temporary_name("MyClass(with description)")

c.new # => #<MyClass(with description):0x0....>

c::M = m
c::M.name #=> "MyClass(with description)::M"

# Assigning to a constant replaces the name with a permanent one
C = c

C.name #=> "C"
C::M.name #=> "C::M"
c.new # => #<C:0x0....>
No documentation available

module Foo; end ^^^^^^^^^^^^^^^

module Foo; end ^^^^^^^^^^^^^^^

Search took: 6ms  ·  Total Results: 3609