Strange behaviour in 'and' with params

I need to convert the user_param HashWithIndifferentAccess in simple
Hash (to convert it in YAML)

I noticed a weird behavior:

Why if I use (in a controller):
user_params = (params[:user_param].to_hash)

user_params is a Hash
as I expected

and if I use:

user_params = (params[:user_param]) and (params[:user_param].to_hash)

user_params is a HashWithIndifferentAccess ?!?

params[:user_param] is NOT empty !

I know that “and” should return the second params:
(1 and 2) return 2

Do someone guess why user_params is a HashWithIndifferentAccess in this
instruction: (params[:user_param]) and (params[:user_param].to_hash) ?

thank you,
Alessandro

On May 28, 11:44 am, Ale Ds [email protected] wrote:

and if I use:

user_params = (params[:user_param]) and (params[:user_param].to_hash)

and has very low precedence, so ruby actually treats this as

(user_params = (params[:user_param])) and
(params[:user_param].to_hash)

&& on the other hand binds more tightly and would do what you expect.

Fred

Frederick C. wrote:

On May 28, 11:44�am, Ale Ds [email protected] wrote:

and has very low precedence, so ruby actually treats this as

(user_params = (params[:user_param])) and
(params[:user_param].to_hash)

&& on the other hand binds more tightly and would do what you expect.

Fred

I will carefully use ‘and’ next time,
or maybe I will use only && :slight_smile:

After your reply, I’ve found this

Thank you very much !
Alessandro DS