Skip to content

Instantly share code, notes, and snippets.

@wesmaldonado
Created February 5, 2010 21:23
Show Gist options
  • Save wesmaldonado/296259 to your computer and use it in GitHub Desktop.
Save wesmaldonado/296259 to your computer and use it in GitHub Desktop.
# From Rails 3 ActiveSupport I believe
class StringInquirer < String
def method_missing(method_name, *arguments)
if method_name.to_s[-1,1] == "?"
self == method_name.to_s[0..-2]
else
super
end
end
end
##
# Creates an adset capable of answering about positions in the set as methods.
# ` AdSet.new("top_rail).top_rail? => true`
#
class AdSet < Array
def initialize(*args)
super(*args)
self.map!{ |v| StringInquirer.new(v) }
self
end
def <<(v)
insert(0, StringInquirer.new(v))
end
alias push <<
alias unshift <<
def method_missing(method_name, *arguments)
return self.any?{ |v| v.send(method_name) }
end
end
cool_ad_set = AdSet.new(['top_rail','right_rail','header_text'])
puts "This AdSet #{cool_ad_set.inspect} contains top_rail? #{cool_ad_set.top_rail?}"
# => This AdSet ["top_rail", "right_rail", "header_text"] contains top_rail? true
puts "This AdSet #{cool_ad_set.inspect} contains random_thing? #{cool_ad_set.random_thing?}"
# => This AdSet ["top_rail", "right_rail", "header_text"] contains random_thing? false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment