DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Nested Methods In Ruby

04.08.2010
| 3750 views |
  • submit to reddit
        Source: <a href="http://twitter.com/igrigorik/status/11803597323">igrigorik</a> [twitter.com]
... ruby's syntax allows for nested methods:
def a; def b; :b; end; b; end; a 
#=> :b

Here's another example:
def a2
  def c
    "hello"
  end
  c
end

a2
#=> "hello" 

Note: Even though the method is nested it still has the same scope as a non-nested method.