is there any way in ruby to do something like:
with thisobject do
....
end
···
--
Posted via http://www.ruby-forum.com/.
is there any way in ruby to do something like:
with thisobject do
....
end
--
Posted via http://www.ruby-forum.com/.
Depends on what you want to do, probably use #instance_eval e.g
irb(main):001:0> "foo".instance_eval do p size end
3
=> 3
Or you can use #tap (1.9)
irb(main):002:0> "foo".tap do |x| p x.size end
3
=> "foo"
Cheers
robert
On Mon, Mar 28, 2011 at 5:05 PM, Mario Ruiz <tcblues@gmail.com> wrote:
is there any way in ruby to do something like:
with thisobject do
....end
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
Do you mean something like this?
def with(arg)
yield arg
end
with [1,2,3] do |i|
p i.size # => 3
end
On Tue, Mar 29, 2011 at 12:05 AM, Mario Ruiz <tcblues@gmail.com> wrote:
is there any way in ruby to do something like:
with thisobject do
....end
--
Posted via http://www.ruby-forum.com/\.
--
Haruka YAGNI
hyagni@gmail.com
You could use
obj.instance_eval do
puts self.inspect
end
or:
obj.instance_eval do |something|
puts something.inspect
end
On Mon, Mar 28, 2011 at 4:05 PM, Mario Ruiz <tcblues@gmail.com> wrote:
is there any way in ruby to do something like:
with thisobject do
....end
Yes, you can accomplish that with instance_eval:
def with(obj, &block)
obj.instance_eval &block
end
with do
p length # => 0
end
-- fxn
On Mon, Mar 28, 2011 at 5:05 PM, Mario Ruiz <tcblues@gmail.com> wrote:
is there any way in ruby to do something like:
with thisobject do
....end
try #instance_eval
eg,
def with o, &block
o.instance_eval &block
end
#=> nil
with "test" do
puts capitalize
end
Test
best regards -botp
Also available in 1.8.7 :).
On Mon, Mar 28, 2011 at 5:13 PM, Robert Klemme <shortcutter@googlemail.com> wrote:
Or you can use #tap (1.9)