Sha256: 9e5d29ac8f71aa72eaaf7035e25ae8beae93da6e3d19a22355fb57ddab50e847

Contents?: true

Size: 1.35 KB

Versions: 47

Compression:

Stored size: 1.35 KB

Contents

# HoboSupport - Metaid

    doctest_require: '../../lib/hobo_support'
{.hidden}

Why the Luck Stiff's essential meta-programming additions to Object. These are probably distributed elsewhere, but they're small enough to throw them in to HoboSupport and remove an external dependency.

## `Object#metaclass`

Returns the "metaclass", "eigenclass" or "singleton class" of a given ruby Object

    >> o = Object.new
    >> def o.foo; ;123; end
    >> o.foo
    => 123
    >> o.metaclass.method_defined?(:foo)
    => true

## `Object#meta_eval`

* `object.meta_eval(string)`
* `object.meta_eval { block }`

Evaluates ruby source or a block in the context of the metaclass.

    >> File.meta_eval "alias_method :b, :basename"
    >> File.b "a/b"
    => "b"

And with a block

    >> File.meta_eval { alias_method :b2, :basename }
    >> File.b2 "a/b"
    => "b"

## `Object#metaclass_eval`

* `object.metaclass_eval(string)`
* `object.metaclass_eval { block }`

Like `#meta_eval`, but does a `class_eval` instead of an `instance_eval`

    >> File.metaclass_eval "def b3(path); basename(path); end"
    >> File.b3 "a/b"
    => "b"

And with a block

    >> File.metaclass_eval { def b4(path); basename(path); end }
    >> File.b4 "a/b"
    => "b"


## `Object#meta_def`

    >> String.meta_def(:backwards_new) { |s| s.reverse }
    >> String.backwards_new "strange"
    => "egnarts"

Version data entries

47 entries across 47 versions & 1 rubygems

Version Path
hobo_support-2.0.0 test/hobosupport/metaid.rdoctest
hobo_support-2.0.0.pre10 test/hobosupport/metaid.rdoctest
hobo_support-2.0.0.pre9 test/hobosupport/metaid.rdoctest
hobo_support-2.0.0.pre8 test/hobosupport/metaid.rdoctest
hobo_support-1.3.3 test/hobosupport/metaid.rdoctest
hobo_support-2.0.0.pre7 test/hobosupport/metaid.rdoctest
hobo_support-2.0.0.pre6 test/hobosupport/metaid.rdoctest
hobo_support-2.0.0.pre5 test/hobosupport/metaid.rdoctest
hobo_support-2.0.0.pre4 test/hobosupport/metaid.rdoctest
hobo_support-1.3.2 test/hobosupport/metaid.rdoctest
hobo_support-1.3.1 test/hobosupport/metaid.rdoctest
hobo_support-2.0.0.pre3 test/hobosupport/metaid.rdoctest
hobo_support-2.0.0.pre2 test/hobosupport/metaid.rdoctest
hobo_support-2.0.0.pre1 test/hobosupport/metaid.rdoctest
hobo_support-1.4.0.pre8 test/hobosupport/metaid.rdoctest
hobo_support-1.4.0.pre7 test/hobosupport/metaid.rdoctest
hobo_support-1.4.0.pre6 test/hobosupport/metaid.rdoctest
hobo_support-1.4.0.pre5 test/hobosupport/metaid.rdoctest
hobo_support-1.4.0.pre4 test/hobosupport/metaid.rdoctest
hobo_support-1.4.0.pre3 test/hobosupport/metaid.rdoctest