Hi all Is there an equivalent to the || thing? var = nil || "blah" # => "blah" var = "" || "blah" # => "" I want something like that: var = nil ||| "blah" # => "blah" var = "" ||| "blah" # => "blah" Is there something like that in Rails? :-) Thanks Josh -- 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 -~----------~----~----~----~------~----~------~--~---
To my knowledge, there is not. var = "blah" if var.blank? will however do what you want On 28/08/2008, at 7:42 AM, Joshua Muheim wrote:> > Hi all > > Is there an equivalent to the || thing? > > var = nil || "blah" > # => "blah" > > var = "" || "blah" > # => "" > > I want something like that: > > var = nil ||| "blah" > # => "blah" > > var = "" ||| "blah" > # => "blah" > > Is there something like that in Rails? :-) > > Thanks > Josh > -- > 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 -~----------~----~----~----~------~----~------~--~---
On Thu, Aug 28, 2008 at 12:12 AM, Joshua Muheim <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Is there an equivalent to the || thing? > > var = nil || "blah" > # => "blah" > > var = "" || "blah" > # => "" > > I want something like that: > > var = nil ||| "blah" > # => "blah" > > var = "" ||| "blah" > # => "blah"This is logically equivalent, though not as pretty var = a.blank? ? b : a --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Xavier Noria wrote:> On Thu, Aug 28, 2008 at 12:12 AM, Joshua Muheim > <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > >> var = nil ||| "blah" >> # => "blah" >> >> var = "" ||| "blah" >> # => "blah" > > This is logically equivalent, though not as pretty > > var = a.blank? ? b : aThanks. But isn''t there a way to extend ruby so it can handle "|||"? -- 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 -~----------~----~----~----~------~----~------~--~---
On Thu, Aug 28, 2008 at 12:32 AM, Joshua Muheim <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Thanks. But isn''t there a way to extend ruby so it can handle "|||"?In Ruby some "operators" are syntactic sugar for methods, for example "<<", or "+". But there''s no way to go in the other direction. That is, you cannot define a method in Object named "|||" and tell Ruby it should be parsed as an operator of such and such arity, etc. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Xavier Noria wrote:> On Thu, Aug 28, 2008 at 12:32 AM, Joshua Muheim > <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > >> Thanks. But isn''t there a way to extend ruby so it can handle "|||"? > > In Ruby some "operators" are syntactic sugar for methods, for example > "<<", or "+". But there''s no way to go in the other direction. That > is, you cannot define a method in Object named "|||" and tell Ruby it > should be parsed as an operator of such and such arity, etc.What a pitty... Thanks though! -- 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 -~----------~----~----~----~------~----~------~--~---
On Thu, Aug 28, 2008 at 12:57 AM, Joshua Muheim <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> What a pitty... Thanks though!Of course if a method is good for you this is doable class Object def bor(other) blank? ? other : self end end nil.bor("blah") # => "blah" ''''.bor({}).bor("blah") # => "blah" [].bor("blah").bor(true) # => "blah" --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Aug 27, 2008, at 6:12 PM, Joshua Muheim wrote:> Hi all > > Is there an equivalent to the || thing? > > var = nil || "blah" > # => "blah" > > var = "" || "blah" > # => "" > > I want something like that: > > var = nil ||| "blah" > # => "blah" > > var = "" ||| "blah" > # => "blah" > > Is there something like that in Rails? :-) > > Thanks > JoshLike the Numeric#nonzero? that is useful in chained comparisons, you can define nonblank? to be similar and get this effect: class String # Allowing a chain like: string_value.nonblank? || ''default value'' def nonblank? self unless blank? end end class NilClass # Allowing a chain like: value.nonblank? || ''default value'' def nonblank? self end # so it plays nicely with Numeric#nonzero? def nonzero? self end end Since you''re already Rails, you''re already ActiveSupport and blank? will be well defined. irb> nil.nonblank? || "blah" => "blah" irb> "".nonblank? || "blah" => "blah" irb> "foo".nonblank? || "blah" => "foo" -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org
Thanks guys, I just found the blank? method. :-) -- 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 -~----------~----~----~----~------~----~------~--~---
In Rails you can now do this: params[:state].presence || params[:country].presence || ''US'' http://apidock.com/rails/Object/presence -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.