0

I'm attempting to debug a Ruby script, but I am unable to access one method as seen below.

  def move_objects(target_folder)
    s3_objects.each do |obj|
      binding.pry
      new_key = s3_folder ? obj.key.sub(s3_folder, target_folder) : "#{target_folder}/#{obj.key}"
      obj.put(metadata: { 'new_key' => 'ok' })
      obj.move_to(bucket: bucket_name, key: new_key)
    end
    self
  end

When I call the method as so in Rails C:

 Courts::Aws::S3Util.new('bucket_name').move_objects(target_folder)

I receive the following error.

NameError: undefined local variable or method `target_folder' for main:Object

What is the appropriate way to access this function to debug and read the new_key data?

  • 3
    If you are not sure what should the value of `target_folder` be then just call it with empty string `Courts::Aws::S3Util.new('bucket_name').move_objects('')` and debug accordingly. – Deepesh Sep 06 '21 at 15:41
  • 4
    `target_folder` is the variable name for the argument passed to the function. You need to give it an actual value when **calling** the method. This applies to all methods, in all programming languages. For example, you could either call `move_objects('example-target-folder')`, or you could assign a local variable first and then call it with that variable: `cdln = "example-target-folder'; ... move_objects(cdln)`. – Tom Lord Sep 06 '21 at 16:18

2 Answers2

0

You should initialize target_folder variable before passing it as an argument to the method

target_folder = "target-folder-name"   # Initialize with appropriate folder name or leave it empty "" (i.e., target_folder = "")

Courts::Aws::S3Util.new('bucket_name').move_objects(target_folder)
Manjunath P
  • 403
  • 3
  • 10
0

Have you tried wrapping the method around a begin / rescue block ?

def move_objects(target_folder)
  begin
    s3_objects.each do |obj|
      binding.pry
      new_key = s3_folder ? obj.key.sub(s3_folder, target_folder) : "#{target_folder}/#{obj.key}"
      obj.put(metadata: { 'new_key' => 'ok' })
      obj.move_to(bucket: bucket_name, key: new_key)
    end
    self
  rescue => e
    binding.pry
  end
end
Sumak
  • 927
  • 7
  • 21