Unable to print a block argument

I am doing some experiment with ruby and I am not sure why the #len
method is not returning the length of the object that I created from
Foo.

class Foo

def self.open(test)
x = new test
block_given? ? yield(test) : test
end

attr_accessor :op

def initialize( op )
@op = op
end

def len
@op.length
end

end

Foo.open(“this”) { |o| puts o.len }

Hi,

You’re passing the string from Foo.new to the block. I guess you wanted
to pass the instance:

def self.open(test)
x = new test
block_given? ? yield(x) : x
end

Thanks, JAn

I the concept was right but I didn’t look close enough. I have modified
it a little bit please see the snippet.

Can you also tell me the name of this pattern as it us used a lot in
Ruby, and what would be a good use case.

Thanks

‘’’

class Foo
attr_accessor :name, :surname, :age

def initialize( name, surname, age )
@name = name
@surname = surname
@age = age
end

def self.open(foo)
x = new “foo”, “bar”, “buzz”
block_given? ? yield(x) : x
end

def len
@op.length
end
end

Foo.open(“this”) do |o|
puts o.name
o.surname = “xxxx”
puts o.surname
puts o.age
end

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 ?

Ja Tse wrote in post #1072005:

Can you also tell me the name of this pattern as it us used a lot in
Ruby, and what would be a good use case.

Which pattern do you mean?

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.

2012/8/11 Ja Tse [email protected]:

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.

– Matma R.

Henry M. wrote in post #1072063:

On 12/08/2012, at 12:24 AM, Ja Tse [email protected] wrote:

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.

On 12/08/2012, at 12:24 AM, Ja Tse [email protected] wrote:

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