The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
EigenCharges

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
Red Handed

Posts: 1158
Nickname: redhanded
Registered: Dec, 2004

Red Handed is a Ruby-focused group blog.
EigenCharges Posted: May 4, 2006 11:04 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Red Handed.
Original Post: EigenCharges
Feed Title: RedHanded
Feed URL: http://redhanded.hobix.com/index.xml
Feed Description: sneaking Ruby through the system
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Red Handed
Latest Posts From RedHanded

Advertisement

A new sleight of syntax for ya:

 class RubyTalk < Filter
 + to("ruby-talk@ruby-lang.org")
 + cc("ruby-talk@ruby-lang.org")
 - from("common.troll@gifted.net")
 end
 class SelfBlog < Filter
 + to("selfblog@hobix.com")
 + subject("secretpassword")
 def process(msg)
 Blog.add_post(msg)
 end
 end

Young ones, see if you can figure out how it’s done before I hand you the actual raw sprinkles of meta magic dust. Specifically: (1) where are the rules getting stored? and (2) how are the operators actually implemented? Just have a think before the spoilers.

First, the Code

 class Filter
 class Rule
 attr_accessor :field, :terms, :action
 def initialize(f, *a)
 @field = f
 @terms = a
 end
 def -@; @action = :reject end
 def +@; @action = :accept end
 end
 class << self
 attr_accessor :rules
 [:from, :to, :cc, :subject].each do |m|
 define_method(m) do |*a|
 r = Rule.new(m, *a)
 (@rules ||= []) << r
 r
 end
 end
 end
 end

To view the rules for mail going into the RubyTalk mailbox:

 RubyTalk.rules.each do |r|
 puts "[#{r.action}] if #{r.field} matches `#{r.terms}'" 
 end

And, the Answers

1. Where are the rules getting stored? The rules are stored in instance variables inside each class. So, not in a RubyTalk object. In the actual RubyTalk class. Right next to the methods.

2. How are the operators actually implemented? By now, you probably can see just fine. Unary operators on the Rule class. The to and from class methods create Rule classes, which then get charged positively and negatively by the operator.

Read: EigenCharges

Topic: Agile Web Development with Rails (2nd Ed) Previous Topic   Next Topic Topic: Protolize: Herramientas esenciales para la web en un solo sitio

Sponsored Links



Google
  Web Artima.com   

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