I do it like this:

http://gist.github.com/53846

Long story short:

>> class Foo
>>   attr_accessor :bar
>>   nillify_blank :bar
>> end
>> f = Foo.new
=> #<Foo:0x4fe8a68>
>> f.bar = ""
=> ""
>> f.bar
=> nil

Then you can use the || operator naturally.

Chris

On Jan 27, 10:04 pm, Mike Gaffney <[email protected]> wrote:
> ifempty?
>
> Pascal Ehlert wrote:
> > Hi,
>
> > How about adding some method #foo instead that returns nil if the
> > string is blank and self otherwise, so that you can do:
>
> > self.email.foo || self.name
>
> > This looks slightly better and more intuitive to me, if we can figure
> > out a descriptive name for the #foo method (I can’t think of any atm,
> > admittedly).
>
> > Cheers
>
> > Pascal
>
> > On 1/28/09 6:45 AM, "Ryan Angilly" <[email protected]> wrote:
>
> >     Hey guys,
>
> >     I find myself doing stuff like this a lot:
>
> >     self.email.blank? ? self.name <http://self.name> : self.email
>
> >     Anyone feel like adding an 'or' method to nil and String (a la
> >     blank?) so that this will work:
>
> >     self.email.or(self.name <http://self.name> )
>
> >     It would be as simple as adding this file to ActiveSupport
>
> >     activesupport/lib/core_ext/or.rb
>
> >     class String
> >     def or(val)
> >     self == "" ? val : self
> >     end
> >     end
>
> >     class NilClass
> >     def or(val)
> >     val
> >     end
> >     end
>
> >     I'll submit a patch of this + tests to LH if people are interested.
>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-core?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to