Name = upload['datafile'].original_filename


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:


It seems that there is an argument “upload”.

But, in the body, when we say:


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 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:

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
puts “Called [] with key #{key.inspect}”

f =
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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs