From: come Date: 2007-08-25T07:45:04+09:00 Subject: Re: Changing "self" for cleaner code Thanks for your anwser. I knew I could write the form "File.open(...) do |x| ... end". I read nearly all books on Ruby/Rails right now ;-). The last I'm reading is "Practical Ruby for System Administration", Apress. A good book, too. I was surprised. But I cant't practice as often as I would, so I miss some automatism. I was asking myself if i couldn't write something like : with my_object do something end instead of : with my_object do |o| o.something end I found it would be a little bit cleaner to write like that. The first answer gave me an answer : I can define "with" myself. Come On 23 aug, 21:34, Robert Klemme wrote: > On 23.08.2007 12:24, come wrote: > > > Yes ! > > I didn't think about using instance_eval this way and define my own > > "with" method... I'm still not confortable with metaprogramming ;-) > > You don't need to. You can simply use instance_eval directly: > > File.open("foo", "w") do |x| > x.instance_eval do > puts "hello" > end > end > > Although I have to say I find this a bit clumsy. I'd prefer > > File.open("foo", "w") do |io| > io.puts "hello" > end > > Even if there were more IO operations I had to do on "io". The piece > above could be easily confused with printing to $defout. Just my 0.02EUR... > > Kind regards > > robert