has anyone done something like this? class NilClass def empty?; true; end def strip; nil; end def [](s); nil; end end which is nice so that you can do this without throwing exception: if !params[:something].empty? if !params[:something][:else].strip.empty? as opposed to this: if params[:something] && !params[:something].empty? if params[:something] && params[:something][:else] && ! params[:something][:else].strip.empty? It seems to work fine for me so far. I''m just wondering what''s the negative effect of doing that? If not then shouldn''t we be doing this :) ? - reynard --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
it seems my previous post didn''t get through for some reason, so sorry if this is duplicated. has anyone done something like this? class NilClass def empty?; true; end def strip; nil; end def [](s); nil; end end so you can do this without throwing exception if !params[:s].empty? if !params[:s][:e].strip.empty? as opposed to: if params[:s] && !params[:s].empty? if params[:s] && params[:s][:e] && !params[:s][:e].strip.empty? I''m just wondering is there negative effects of doing this? if not then shouldn''t we be doing this. it makes your code look a lot nicer doesn''t it? :) - reynard --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
hmm is google group broken? my post never seems to get through. did anyone get this message? On 7/18/07, Reynard <reynardmh-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > it seems my previous post didn''t get through for some reason, so sorry > if this is duplicated. > > has anyone done something like this? > > class NilClass > def empty?; true; end > def strip; nil; end > def [](s); nil; end > end > > so you can do this without throwing exception > if !params[:s].empty? > if !params[:s][:e].strip.empty? > > as opposed to: > if params[:s] && !params[:s].empty? > if params[:s] && params[:s][:e] && !params[:s][:e].strip.empty? > > > I''m just wondering is there negative effects of doing this? if not > then shouldn''t we be doing this. it makes your code look a lot nicer > doesn''t it? :) > > - reynard > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Reynard wrote:> has anyone done something like this? > > class NilClass > def empty?; true; end > def strip; nil; end > def [](s); nil; end > end > > which is nice so that you can do this without throwing exception: > if !params[:something].empty? > if !params[:something][:else].strip.empty? > > as opposed to this: > if params[:something] && !params[:something].empty? > if params[:something] && params[:something][:else] && ! > params[:something][:else].strip.empty? > > It seems to work fine for me so far. I''m just wondering what''s the > negative effect of doing that? If not then shouldn''t we be doing > this :) ? > > - reynardYou disable errors that make perfect sense. The problem is that you really get into a world of pain if you do not expect any nil values is you Hash (or the expected Hash to be nil instead). This is when you want to see those errors. I see your problem with if params[:something][:else], but your first example is already solved by such a technique: Rails has #blank? which is defined on nil. if params[:something] && !params[:something].empty? => unless params[:something].blank? -- 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 -~----------~----~----~----~------~----~------~--~---
I agree, blank? is the way. otherwise I suggest; class NilClass def method_missing(method_name, *args) return true end end Florian Gilcher wrote:> Reynard wrote: >> has anyone done something like this? >> >> class NilClass >> def empty?; true; end >> def strip; nil; end >> def [](s); nil; end >> end-- 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 -~----------~----~----~----~------~----~------~--~---
ah I knew there must be a better way. It''s strange that I cannot find blank? in the ruby or rails api documentation. Thanks a lot, - reynard On Jul 19, 5:04 am, Matthew Rudy <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I agree, > blank? is the way. > > otherwise I suggest; > > class NilClass > def method_missing(method_name, *args) > return true > end > end > > Florian Gilcher wrote: > > Reynard wrote: > >> has anyone done something like this? > > >> class NilClass > >> def empty?; true; end > >> def strip; nil; end > >> def [](s); nil; end > >> end > > -- > 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 -~----------~----~----~----~------~----~------~--~---