Sche Daniel wrote:
> Hello all,
> 
> open is singleton method of File and can be used in two ways
> 
> # 1 way
> f = File.open("data")
> f.readline until f.lineno == 10
> puts f.readline
> f.close
> 
> # 2 way
> File.open("data") do |f|
>     f.readline until f.lineno == 10
>     puts f.readline
> end
> 
> I am trying to mimic this with my own classes
> 
> irb(main):001:0> class Resource
> irb(main):002:1> @@cnt=0
> irb(main):003:1> def initialize
> irb(main):004:2> @id =  @@cnt
> irb(main):005:2> @@cnt += 1
> irb(main):006:2> end
> irb(main):007:1> def id;@id;end
> irb(main):008:1> end
> => nil
> irb(main):009:0> class Q
> irb(main):010:1> def Q.get &block
> irb(main):011:2> r = Resource.new
> irb(main):012:2> if Kernel::block_given?
> irb(main):013:3> block.call(r)
> irb(main):014:3> else
> irb(main):015:3* return r
> irb(main):016:3> end
> irb(main):017:2> end
> irb(main):018:1> end
> => nil
> irb(main):019:0> Q.get
> => #<Resource:0x40206fe0 @id=0>
> irb(main):020:0> Q.get {|r| puts r.id}
> 1
> => nil
> irb(main):021:0>
> 
> 
> I am interested in further suggestions
> is my implementation close to that of File.open?

You're missing the cleanup part when called with a block.  The crucial 
bit is that the block form allows proper cleanup under all circumstances 
by using begin - ensure.

Kind regards

	robert