Class: Refinement

Inherits:
Module show all
Defined in:
class.c,
class.c,
object.c

Overview


Refinement is a class of the +self+ (current context) inside +refine+
statement. It allows to import methods from other modules, see #import_methods.

Instance Method Summary collapse

Methods inherited from Module

#<, #<=, #<=>, #==, #===, #>, #>=, #alias_method, #ancestors, #append_features, #attr, #attr_accessor, #attr_reader, #attr_writer, #autoload, #autoload?, #class_eval, #class_exec, #class_variable_defined?, #class_variable_get, #class_variable_set, #class_variables, #const_added, #const_defined?, #const_get, #const_missing, #const_set, #const_source_location, constants, #constants, #define_method, #deprecate_constant, #extend_object, #extended, #freeze, #include, #include?, #included, #included_modules, #initialize, #initialize_clone, #initialize_copy, #instance_method, #instance_methods, #method_added, #method_defined?, #method_removed, #method_undefined, #module_eval, #module_exec, #module_function, #name, nesting, #prepend, #prepend_features, #prepended, #private, #private_class_method, #private_constant, #private_instance_methods, #private_method_defined?, #protected, #protected_instance_methods, #protected_method_defined?, #public, #public_class_method, #public_constant, #public_instance_method, #public_instance_methods, #public_method_defined?, #refine, #refinements, #remove_class_variable, #remove_const, #remove_method, #ruby2_keywords, #set_temporary_name, #singleton_class?, #to_s, #undef_method, #undefined_instance_methods, used_modules, used_refinements, #using

Constructor Details

This class inherits a constructor from Module

Instance Method Details

#import_methods(*args) ⇒ Object (private)

call-seq: import_methods(module, ...) -> self

Imports methods from modules. Unlike Module#include, Refinement#import_methods copies methods and adds them into the refinement, so the refinement is activated in the imported methods.

Note that due to method copying, only methods defined in Ruby code can be imported.

module StrUtils
  def indent(level)
    ' ' * level + self
  end
end

module M
  refine String do
    import_methods StrUtils
  end
end

using M
"foo".indent(3)
#=> "   foo"

module M
  refine String do
    import_methods Enumerable
    # Can't import method which is not defined with Ruby code: Enumerable#drop
  end
end


873
874
875
876
# File 'class.c', line 873

static VALUE
refinement_import_methods(int argc, VALUE *argv, VALUE refinement)
{
}

#targetObject

Return the class or module refined by the receiver.

module M
 refine String do
 end
end

M.refinements[0].target # => String


1418
1419
1420
1421
1422
1423
1424
1425
# File 'eval.c', line 1418

VALUE
rb_refinement_module_get_refined_class(VALUE module)
{
    ID id_refined_class;

    CONST_ID(id_refined_class, "__refined_class__");
    return rb_attr_get(module, id_refined_class);
}