Skip to content

Instantly share code, notes, and snippets.

@AlexWayfer
Created November 9, 2016 07:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlexWayfer/f40ce3a4be74630d020455ef9d8a698d to your computer and use it in GitHub Desktop.
Save AlexWayfer/f40ce3a4be74630d020455ef9d8a698d to your computer and use it in GitHub Desktop.
Checking of object empty
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