Credit: | Daniel Schierbeck |
from_string | -> | from_symbol |
new | -> | postinitialize_new |
basename | -> | demodulize |
allocate | -> | _allocate_without_cut |
module_load | -> | class_load |
module_require | -> | class_require |
Creates a class-variable attribute that can be accessed both on an instance and class level.
NOTE These used to be a Module methods. But turns out these to work as expected when included. The class-level method is not carried along. So the are now just class methods. Accordingly, mattr will eventually be deprecated so use cattr instead.
Creates a class-variable attr_accessor that can be accessed both on an instance and class level.
class MyClass cattr_accessor :a end MyClass.a = 10 MyClass.a #=> 10 mc = MyClass.new mc.a #=> 10
Creates a class-variable attr_reader that can be accessed both on an instance and class level.
class MyClass @@a = 10 cattr_reader :a end MyClass.a #=> 10 MyClass.new.a #=> 10
Creates a class-variable attr_writer that can be accessed both on an instance and class level.
class MyClass cattr_writer :a def a @@a end end MyClass.a = 10 MyClass.a #=> 10 MyClass.new.a = 29 MyClass.a #=> 29
Returns true, if this class can be used to create an instance from a serialised JSON string. The class has to implement a class method json_create that expects a hash as first parameter, which includes the required data.
Converts a class name to a suitable method name
My::ClassName.method_name => "my__class_name"
Inspired by facets/core/string/underscore.
Prepend an aspect module to a class.
class Firetruck
def put_out_fire(option) "Put out #{option}" end
end
module FastFiretruck
def put_out_fire(option) super("very #{option}!") end
end
Firetruck.prepend(FastFiretruck)
ft = Firetruck.new ft.put_out_fire(‘fast’) #=> "Put out very fast!"
Convert instatiation of a class into a Proc.
class Person def initialize(name) @name = name end def inspect @name.to_str end end %w(john bob jane hans).map(&Person) => [john, bob, jane, hans]