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''} Would become: 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. With kind regads --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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.-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Yeah, ruby doesn''t care about spaces, but it does care about new lines. Which is good, at least it doesn''t rely on semi-colons. for a multiline declaration with clarity of commas tmp = { :message => ''tmp'' , :name => ''tmp'' , :email => ''tmp'' }> 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''} > > Would become: > 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''-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2007-Jul-14 13:03 UTC
Re: Symbols are puzzling me and not only symbols
Hi -- On Sat, 14 Jul 2007, Matthew Rudy wrote:> > Yeah, ruby doesn''t care about spaces, but it does care about new lines. > Which is good, at least it doesn''t rely on semi-colons.There are some cases where spaces matter; for example: irb(main):002:0> puts (3) + 2 5 => nil irb(main):003:0> puts(3) + 2 3 NoMethodError: undefined method `+'' for nil:NilClass In the second one, it evaluated puts(3) (including actually puts''ing 3) and then tries to add 2 to it, which fails. Also: irb(main):004:0> puts puts (3) (irb):4: warning: don''t put space before argument parentheses 3 nil which does the same as puts puts(3) but gives you that warning.> for a multiline declaration with clarity of commas > > tmp = { > :message => ''tmp'' , > :name => ''tmp'' , > :email => ''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. David -- * Books: RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242) RUBY FOR RAILS (http://www.manning.com/black) * Ruby/Rails training & consulting: Ruby Power and Light, LLC (http://www.rubypal.com) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> >> for a multiline declaration with clarity of commas >> >> tmp = { >> :message => ''tmp'' , >> :name => ''tmp'' , >> :email => ''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; tmp = { :message => "tmp", :name => "tmp", :email => "tmp", } that''s how I like it. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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: [quote] this class has dubious semantics and we only have it so that people can write params[:key] instead of params[''key''] [/quote] does not inspire confidence. On Jul 14, 2:36 pm, Matthew Rudy <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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 tosymbolsvs. 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. > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hmmm, alright. I still don''t understand why the parser does not just run until it encounters an other curly brace (}). But I can live with it. On Jul 14, 2:41 pm, Matthew Rudy <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Yeah, ruby doesn''t care about spaces, but it does care about new lines. > Which is good, at least it doesn''t rely on semi-colons. > > for a multiline declaration with clarity of commas > > tmp = { > :message => ''tmp'' , > :name => ''tmp'' , > :email => ''tmp'' > } > > > 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''} > > > Would become: > > 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'' > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Interesing example! I should use that in class. Thanks. On Jul 14, 3:03 pm, dbl...-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org wrote:> Hi -- > > On Sat, 14 Jul 2007, Matthew Rudy wrote: > > > Yeah, ruby doesn''t care about spaces, but it does care about new lines. > > Which is good, at least it doesn''t rely on semi-colons. > > There are some cases where spaces matter; for example: > > irb(main):002:0> puts (3) + 2 > 5 > => nil > irb(main):003:0> puts(3) + 2 > 3 > NoMethodError: undefined method `+'' for nil:NilClass > > In the second one, it evaluated puts(3) (including actually puts''ing > 3) and then tries to add 2 to it, which fails. > > Also: > > irb(main):004:0> puts puts (3) > (irb):4: warning: don''t put space before argument parentheses > 3 > nil > > which does the same as puts puts(3) but gives you that warning. > > > for a multiline declaration with clarity of commas > > > tmp = { > > :message => ''tmp'' , > > :name => ''tmp'' , > > :email => ''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. > > David > > -- > * Books: > RAILS ROUTING (new!http://www.awprofessional.com/title/0321509242) > RUBY FOR RAILS (http://www.manning.com/black) > * Ruby/Rails training > & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---