On Sun, Feb 10, 2013 at 9:25 PM, Joel Pearson <lists / ruby-forum.com> wrote:
> I've looked around but I couldn't find anything helpful on this,
> although I'm sure it's been covered before:
> Does Ruby have an equivalent to the "With / End With" syntax helper in
> VB? It would be quite helpful when I need to execute a series of methods
> on an object, but for various reasons (e.g. testing individual returns
> or methods which may return nil) I don't want to chain them together.
>
> To illustrate, here's the VB syntax I'm referring to:
>
> With my_object
>   .do1
>   .do2
>   .do3
> End With

my_object.instance_eval do
  # self points to my_object here
  do1
  do2
  do3
end

You can of course use a different method name, i.e. alias
#instance_eval to #with.

However, there is one gotcha

foo.bar = 99

cannot be translated to

foo.instance_eval do
  bar = 99
end

because bar will be a local variable then.  Instead you have to use

foo.instance_eval do
  self.bar = 99
end

Kind regards

robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/