Created
November 25, 2011 18:11
-
-
Save alkema/1394104 to your computer and use it in GitHub Desktop.
testing ruby object for attribute presence/absence
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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