Is it possible to refer to a parameter passed to a method within the passed block in ruby?

block, ruby

Solution

Not in general; it's totally up to the method itself what arguments the block gets called with, and by the time `each` has been called (which calls your block), the fact that the string `'some_directory'` was passed to `Dir.new` has been long forgotten, i.e. they're quite separate things.

You can do something like this, though:

Dir.new(my_dir = 'some_directory').each do |file|
    puts "#{my_dir} contains #{file}"
end

Problem

I hope I am not repeating anyone here, but I have been searching google and here and not coming up with anything. This question is really more a matter of "sexifying" my code. What I am specifically trying to do is this: ``` Dir.new('some_directory').each do |file| # is there a way to refer to the string 'some_directory' via a method or variable? end ``` Thanks!

Original source