Question: In CoffeeScript:

```
f = ->
  v = 5
  g = ->
    v
  g()

f() # returns 5 as expected

```

In Ruby:

```
def f
  v = 5
  def g
    v # undefined local variable or method `v' for main:Object (NameError)
  end
  g
end
f

```

Okay, so apparently JavaScript functions are set up to capture variables in the scope they are created in, but Ruby's methods are not. Is there a way to make Ruby methods behave like JavaScript functions in this regard?

Answer: 
Short answer, no. Ruby's functions have different scoping rules than Javascript functions.

Longer answer is that you can define objects in Ruby that preserve the scope in which they are defined (called a closure). These are called lambdas, Procs, and blocks in Ruby. Look [here](http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/) for a quick overview.

Edit: blocks are not Ruby objects, but they can be converted into Procs, which are objects.
To add to the other answers, for most consistency with your CoffeeScript code, you would probably write:

```
f = lambda do
  v = 5

  g = lambda do
    v
  end

  g[]
end
f[]

```