Dear all,
I’ve got two burning questions. Both regard the manipulation of the
params hash.
Imagine:
params = {“commit”=>“Add comment”, “comment”=>{“message”=>“foo”,
“name”=>“bar”, “answer”=>“2”, “email”=>“foo@bar”}}
So the params hash has two keys, one(‘commit’) points to a string the
other(‘comment’) to an other hash.
Normally I can do something like; params[:id] or whatever, now if I do
that, params[:commit] it returns nil! Why?? params[‘commit’] works as
expected.
On to the second question, when I have a large array or hash I used
to(In Java/PHP etc) split them up in several lines. For example:
tmp = {:message => ‘tmp’ ,:name => ‘tmp’ ,:email => ‘tmp’}
I find this eaasier to read and easier to check for comma related
errors. However Ruby fails to recognise this, failing with the error:
syntax error, unexpected ‘,’, expecting ‘}’
,:name => ‘tmp’
^
Ruby is better kidding me. The interpreter doesn’t handle whitespace??
Any comments on what I am doing wrong or why not ignoring whitespace
is a good idea, I’d love to hear.
ah…
what you experienced before with params[“id”] and params[:id] returned
the same,
was in fact the magic of rails,
and the HashWithIndifferentAccess,
and extension of hash,
which reacts indifferently to symbols vs. strings.
try;
params = HashWithIndifferentAccess.new
params[:id] = “this is the id”
params[“id”] will return “this is the id”
params[:id] will return “this is the id”
alternatively.
params = {:id => “this is the id”, :commit => “this is the
commit”}.with_indifferent_access.
harm wrote:
Dear all,
I’ve got two burning questions. Both regard the manipulation of the
params hash.
Imagine:
params = {“commit”=>“Add comment”, “comment”=>{“message”=>“foo”,
“name”=>“bar”, “answer”=>“2”, “email”=>“foo@bar”}}
So the params hash has two keys, one(‘commit’) points to a string the
other(‘comment’) to an other hash.
Normally I can do something like; params[:id] or whatever, now if I do
that, params[:commit] it returns nil! Why?? params[‘commit’] works as
expected.
On to the second question, when I have a large array or hash I used
to(In Java/PHP etc) split them up in several lines. For example:
tmp = {:message => ‘tmp’ ,:name => ‘tmp’ ,:email => ‘tmp’}
I find this eaasier to read and easier to check for comma related
errors. However Ruby fails to recognise this, failing with the error:
syntax error, unexpected ‘,’, expecting ‘}’
,:name => ‘tmp’
At the very great risk of sounding curmudgeonly, could I put in a
heartfelt plea for people not to do that? It looks bizarre, and seems
to invite scrutiny in case there’s been some kind of mistake or
omission.
To be honest,
the way I like to declare hashes is like this;
At the very great risk of sounding curmudgeonly, could I put in a
heartfelt plea for people not to do that? It looks bizarre, and seems
to invite scrutiny in case there’s been some kind of mistake or
omission.
Aha, but why does that scheme break down? Knowing that would prevent
me from running future problems. The class documentation()http:// api.rubyonrails.org/classes/HashWithIndifferentAccess.html) does not
bring me very far either. Especially the line: