Forum: Ruby Unable to print a block argument.

Posted by Ja Tse (mxruby)
on 2012-08-10 21:22
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 }
Posted by Jan E. (jacques1)
on 2012-08-10 21:36
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
Posted by Ja Tse (mxruby)
on 2012-08-10 21:53
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
Posted by Jan E. (jacques1)
on 2012-08-11 04:32
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?
Posted by Ja Tse (mxruby)
on 2012-08-11 14:24
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 ?
Posted by Bartosz DziewoƄski (matmarex)
on 2012-08-11 15:24
(Received via mailing list)
2012/8/11 Ja Tse <lists@ruby-forum.com>:
> 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 Rex
Posted by Jan E. (jacques1)
on 2012-08-11 15:25
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.
Posted by Henry Maddocks (Guest)
on 2012-08-12 01:38
(Received via mailing list)
On 12/08/2012, at 12:24 AM, Ja Tse <lists@ruby-forum.com> 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
Posted by Ja Tse (mxruby)
on 2012-08-12 10:13
Henry Maddocks wrote in post #1072063:
> On 12/08/2012, at 12:24 AM, Ja Tse <lists@ruby-forum.com> 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.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.