Saturday, 15 June 2013

ruby - assign/replace params hash in rails -



ruby - assign/replace params hash in rails -

i have code sequence below in rails controller action. before if, params contains request parameters, expected. after it, params nil. can please explain happening here?

if false params = {:user => {:name => "user", :comment => 'comment'}} end

thank you.

the params contains request parameters method phone call returns hash containing parameters. params = line assigning local variable called params.

after if false block, ruby has seen local params variable when refer params later in method local variable has precedence on calling method of same name. because params = assignment within if false block local variable never assigned value local variable nil.

if effort refer local variable before assigning nameerror:

irb(main):001:0> baz nameerror: undefined local variable or method `baz' main:object (irb):1

however if there assignment variable isn't in code execution path ruby has created local variable value nil.

irb(main):007:0> baz = "example" if false => nil irb(main):008:0> baz => nil

ruby-on-rails ruby

No comments:

Post a Comment