Reputation: 59571
Consider the simple following code:
post '/xxx' do
puts params
end
this works fine. Now consider the following modification
post '/xxx' do
params = params
puts params
end
Now params is nil!! I am new to Ruby, and have no idea why this behaviour is happening. Obviously I do not want to execute the useless params = params
expression, but I was trying to to something a little more complicated involving modifying params
and found that it always becomes nil
.
Upvotes: 0
Views: 397
Reputation: 30023
In the first version, you're calling a method called params
and passing its return value to puts
.
In the second version, you're creating a local variable called params
(which hides the method with the same name) and assigning it to itself.
Consider the following example:
def foo
1
end
p foo # outputs `1`
foo = foo
p foo # outputs `nil`
It might not be obvious that this is happening, because in Ruby accessing a local variable and calling a method on self
look exactly the same.
Upvotes: 5