Not sure how to put it just want to know, wat’s the advantage of
creating an object through a proxy method in this case #open vs just
creating an object with the constructor Foo.new ?
It makes no sense for normal objects. But if an object is for example
only meant to be used temporarily and then closed or cleared (like a
file handle or a database connection), this pattern does make sense.
For example, File.open creates a File instance, passes it to the block
and automatically closes the file handle after the block has been
executed. If you use the normal “new” method, you have to be careful to
close the handle afterwards.
Not sure how to put it just want to know, wat’s the advantage of
creating an object through a proxy method in this case #open vs just
creating an object with the constructor Foo.new ?
It might be used if you would have to explicitly call some
“destructor” method on the object after you finish using it.
E.g. the block form of File.open:
File.open(…){|f| do_stuff(f) }
… can be used instead of the longer and more error-prone (if you
forget to close the file):
f = File.open(…)
do_stuff(f)
f.close
I don’t think there is any advantage if there is no such “destructor”
method for your object.
Not sure how to put it just want to know, wat’s the advantage of
creating an object through a proxy method in this case #open vs just
creating an object with the constructor Foo.new ?
You have partially implemented two patterns…
Factory Method; A class method that returns an instance of the class
based on the inputs. Your method always returns the same objectso there
is no point creating another method to do this. Just use #new.
Around Method; When accessing a resource that requires setup and tear
down, eg. File open / file close, you can write a method that initialzes
the resource, yields the resource to a block then clens up the resource
when the block returns. Your method doesn’t do this either but it’s
probably what you were thinking of as it is a very common Ruby pattern.
Henry
Thanks Henry and everyone,
I knew my code was not perfect it was just a starting point and I wanted
someone to elaborate more on the subject. Thanks everyone I have a
better understanding now.
Not sure how to put it just want to know, wat’s the advantage of
creating an object through a proxy method in this case #open vs just
creating an object with the constructor Foo.new ?
You have partially implemented two patterns…
Factory Method; A class method that returns an instance of the class
based on the inputs. Your method always returns the same objectso there
is no point creating another method to do this. Just use #new.
Around Method; When accessing a resource that requires setup and tear
down, eg. File open / file close, you can write a method that initialzes
the resource, yields the resource to a block then clens up the resource
when the block returns. Your method doesn’t do this either but it’s
probably what you were thinking of as it is a very common Ruby pattern.
Henry
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.