Created
November 9, 2016 07:17
-
-
Save AlexWayfer/f40ce3a4be74630d020455ef9d8a698d to your computer and use it in GitHub Desktop.
Checking of object empty
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 MyObject | |
attr_accessor :foo, :bar | |
def initialize(foo = nil, bar = nil) | |
self.foo = foo | |
self.bar = bar | |
end | |
def empty? | |
foo.nil? || foo.empty? | |
end | |
end | |
def object_by_reference(object) | |
object if object.empty? | |
end | |
def new_object(object) | |
MyObject.new if object.empty? | |
end | |
object = MyObject.new('', 'baz') | |
p object_by_reference(object) # will have `bar: 'baz'` | |
p new_object(object) # will have nothing |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment