Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby: can a block affect local variables in a method?

Tags:

closures

ruby

I'm just learning ruby and trying to understand the scope of code executed in blocks. For example, I want to be able to create a block that affects the method that it is attached to, like so:

def test(&block)
  block.call() if block_given?
  puts "in test, foo is #{foo}"
  puts "in test, bar is #{bar}"
end

test() {
  foo="this is foo"
  bar="this is bar"
}

In this case I don't want to have to modify the block at all -- I want to be able to write it using simple variable references and no parameters. Only by making changes to the 'test' method in the above example, is it possible to access the variables defined in the block?

Again, the goal is to leave the block unmodified, but be able to access the created variables from within 'test' after the block executes.

like image 448
Bryan Oakley Avatar asked Jan 07 '09 22:01

Bryan Oakley


People also ask

What is difference between block and method in Ruby?

The only difference is that method has a name but not block and arguments passed between brackets () to method but in block, arguments passed between pipes ||. How block return values: Actually block returns the value which are returned by the method on which it is called. Example: Ruby.

How do blocks work in Ruby?

Ruby blocks are anonymous functions that can be passed into methods. Blocks are enclosed in a do-end statement or curly braces {}. do-end is usually used for blocks that span through multiple lines while {} is used for single line blocks. Blocks can have arguments which should be defined between two pipe | characters.

What is a block argument in Ruby?

What is a ruby block? A ruby block is one or more lines of code that you put inside the do and end keywords (or { and } for inline blocks). It allows you to group code into a standalone unit that you can use as a method argument.

What do Ruby blocks return?

You cannot do that in Ruby. The return keyword always returns from the method or lambda in the current context. In blocks, it will return from the method in which the closure was defined. It cannot be made to return from the calling method or lambda.


1 Answers

First of all, block.call() is done with yield, and you don't need the &block parameter that way.

You can't normally do what you want, blocks are bound when they are created, and inside the block you can see the local variables defined at that moment; the easiest way to do what you want, which is not how you will use blocks normally, is this:

def test()
  foo = yield if block_given?
  puts "in test, foo is #{foo}"
end

test() {
  foo="this is foo"
}

But that's only a side effect because foo is "returned" by the block. If you instead do this:

def test()
  foo = yield if block_given?
  puts "in test, foo is #{foo}"
end

test() {
  foo="this is foo"
  "ha ha, no foo for you"
}

You'll notice that it does something different.

Here's more magic:

def test(&block)
   foo = eval "foo", block.binding
   puts foo
   block.call
   foo = eval "foo", block.binding
   puts foo
end

foo = "before test"
test() {
  foo = "after test"
  "ha ha, no foo for you"
}

That would kind of work, but it breaks if you remove foo = "before test" because foo becomes a local variable in the block and does not exist in the binding.

Summary: you can't access local variables from a block, just the locals where the block was defined and the return value of the block.

Even this won't work:

def test(&block)
   eval "foo = 'go fish'", block.binding
   block.call
   bar = eval "foo", block.binding
   puts bar
end

because the foo in the binding is different from the local in the block (I didn't know this, thanks).

like image 128
krusty.ar Avatar answered Sep 22 '22 19:09

krusty.ar