The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Attributes Revisited: Booleans

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Matt Williams

Posts: 466
Nickname: aetherical
Registered: Feb, 2008

Matt Williams is a jack-of-all trades living in Columbus, OH.
Attributes Revisited: Booleans Posted: May 27, 2008 4:27 PM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Matt Williams.
Original Post: Attributes Revisited: Booleans
Feed Title: Ruby Blender
Feed URL: http://feeds2.feedburner.com/RubyBlender
Feed Description: This blog contains short-ish ruby tips, hints, and techniques.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Matt Williams
Latest Posts From Ruby Blender

Advertisement

While writing some code last night, I realized that I'd left out something from my attributes -- the ability to do booleans such as obj.nil? and have the boolean method created for me. So, the new, improved attributes code -- now with boolean action follows:

class Object
  def self.attribute(*arg,&block)
    (name, default) = arg
    short_name = name.to_s.sub(/\?/,"")
    self.send(:define_method, name) {
      if instance_variables.include? "@#{short_name}"
           self.instance_eval "@#{short_name}"
      else
        if block_given?
          instance_eval &block
        else
          default
        end
      end
    }
    self.send(:define_method, "#{short_name}="){ |value|
      self.instance_eval "@#{short_name} = value"
    }
  end
end

I wonder what other behaviours I can add...... just kidding.

Read: Attributes Revisited: Booleans

Topic: Capturando temperatura utilizando Sentilla Perk (II) Previous Topic   Next Topic Topic: Coming to Portland for RailsConf or CabooseConf

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use