GithubHelp home page GithubHelp logo

meta_programming's Introduction

Extend and define the attribute method

To define a single method 'attribute' which behaves much like the built-in 'attr', but whose properties require delving deep into the depths of meta-ruby. Usage of the 'attribute' method follows the general form of

       class C
         attribute 'a'
       end
       
       o = C::new
       o.a = 42  # setter - sets @a
       o.a       # getter - gets @a 
       o.a?      # query  - true if @a

but reaches much farther than the standard 'attr' method. 'attribute' must provide getter, setter, and query to instances

      def koan_1
        c = Class::new {
          attribute 'a'
        }

        o = c::new

        assert{ not o.a? }
        assert{ o.a = 42 }
        assert{ o.a == 42 }
        assert{ o.a? }
      end

'attribute' must provide a method for providing a default value as hash

      def koan_6
        c = Class::new {
          attribute 'a' => 42
        }

        o = c::new

        assert{ o.a == 42 }
        assert{ o.a? }
        assert{ (o.a = nil) == nil }
        assert{ not o.a? }
      end

'attribute' must provide a method for providing a default value as block which is evaluated at instance level

      def koan_7
        c = Class::new {
          attribute('a'){ fortytwo }
          def fortytwo
            42
          end
        }

        o = c::new

        assert{ o.a == 42 }
        assert{ o.a? }
        assert{ (o.a = nil) == nil }
        assert{ not o.a? }
      end

Solution

We can extend Object with a method that relies on methods definded in module and the method is available in places where it really didn't ought to be. and then we can use class_eval(), which just executes the code in the context of the class where we can define our setter, getter and querier there.

meta_programming's People

Contributors

judywu29 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.