As far as I know, defining a named function within a function does not give you access to any local variables.
What you can do instead is use a Proc:
def foo(x)
bar = lambda do
puts x
end
bar.call
42
end
foo(5)
Related Contents:
- Ruby: merge nested hash
- class
- How do I pick randomly from an array?
- When monkey patching an instance method, can you call the overridden method from the new implementation?
- What is the difference between include and extend in Ruby?
- How to check whether a string contains a substring in Ruby
- How do I remove blank elements from an array?
- find_spec_for_exe’: can’t find gem bundler (>= 0.a) (Gem::GemNotFoundException)
- Running pod setup gives me “bad interpreter: No such file or directory” error
- How do I get ruby to print a full backtrace instead of a truncated one?
- Ruby send vs __send__
- When to use nested classes and classes nested in modules?
- Get file name and extension in Ruby
- Need a simple explanation of the inject method
- Sorting an array of objects in Ruby by object attribute?
- Can you supply arguments to the map(&:method) syntax in Ruby?
- Ideal Ruby project structure
- What is the difference between gsub and sub methods for Ruby Strings
- Ruby regular expression using variable name
- macOS Mojave ‘ruby/config.h’ file not found
- Continuously read from STDOUT of external process in Ruby
- Ruby – mapping an array to hashmap
- therubyracer – Gem::Ext::BuildError: ERROR: Failed to build gem native extension
- Ruby: Proc#call vs yield
- OpenSSL vs GPG for encrypting off-site backups? [closed]
- Ruby: Is it possible to define a class method in a module?
- How to solve “/usr/bin/env: ruby_executable_hooks: No such file or directory”?
- Using Instance Variables in Class Methods – Ruby
- Why is it not a good idea to dynamically create a lot of symbols in ruby (for versions before 2.2)?
- Cross-platform means of getting user’s home directory in Ruby?
- How to read whole file in Ruby?
- Parse command line arguments in a Ruby script
- Ruby: define_method vs. def
- Insecure world writable dir /Users/username in PATH, mode 040777 when running Ruby commands
- Ruby on Rails Switch [duplicate]
- First element of an array with condition
- Converting upper-case string into title-case using Ruby
- Which Ruby memoize pattern does ActiveSupport::Memoizable refer to?
- Installing Nokogiri on OSX 10.10 Yosemite
- What is a Ruby gem?
- Convert Ruby Hash into YAML
- How to use “gets” and “gets.chomp” in Ruby
- Set Ruby variable if it is not already defined
- WARNING: Can’t verify CSRF token authenticity in case of API development
- Vagrant – how to have host platform specific provisioning steps
- Broken pipe (Errno::EPIPE)
- Uninstalling all gems Ruby 2.0.0
- Determining if an array of strings contains a certain substring in ruby
- hash[‘key’] to hash.key in Ruby
- How do you assign a variable with the result of a if..else block?