Skip to content

Instantly share code, notes, and snippets.

@alkema
Created November 25, 2011 18:11
Show Gist options
  • Save alkema/1394104 to your computer and use it in GitHub Desktop.
Save alkema/1394104 to your computer and use it in GitHub Desktop.
testing ruby object for attribute presence/absence
class Widget
attr_accessor :circumference
attr_accessor :colour
attr_accessor :mass
def initialize(attributes)
attributes.each do |key, value|
m = "#{key}=".to_sym
self.send(m, value) if self.respond_to?(m)
end
end
end
widget = Widget.new({circumference:1})
# test for presence of attributes
puts widget.circumference.nil?
puts widget.colour.nil?
% ruby widget.rb
false
true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment