Name = upload['datafile'].original_filename

At: Ruby on Rails - File Uploading | Tutorialspoint

And, regarding:

name = upload[‘datafile’].original_filename

It mentions that “upload” is a CGI object.

If you look at the header of the function this line is part of:

def self.save(upload)

It seems that there is an argument “upload”.

But, in the body, when we say:

upload[‘datafile’]

Is “upload” here the same as the passed argument?

And, what about [‘datafile’], what does it represent here? And, from
where is it passed?

Thanks.

Thanks a lot @Brian for this nice clarification.

Abder-Rahman A. wrote:

Is “upload” here the same as the passed argument?

Yes. Just to be clear, the code here is:

def self.save(upload)
name = upload[‘datafile’].original_filename

And, what about [‘datafile’], what does it represent here?

It’s a method call on the object passed as upload. You are calling the
method called ‘[]’, and passing the string ‘datafile’ as the argument.

What this actually does, depends on what the object ‘upload’ is. All you
can tell from the above is that it implements a method called [] which
takes a string argument. It could be a Hash, for example, in which case
upload[‘datafile’] would retrieve the value keyed by ‘datafile’.

But it could be some other custom object. For example:

class Foo
def
puts “Called [] with key #{key.inspect}”
“hello”
end
end

f = Foo.new
result = f[“datafile”] # Called [] with key “datafile”
puts result # hello

That’s the joy of duck-typing. You don’t care what class an object is,
only what methods it responds to.