I just found out google groups was bouncing my emails because they were
coming from a different address, so to sum them all up in one:
I also realized that I do crap like this a lot too:
self.owner_object_association ? self.owner_object_association :
default_thing
Where this would give me:
self.owner_object_association.or_if_blank? default_thing
Maybe just add it to Object?
class Object
def or_if_blank?(val)
if respond_to? :blank?
return self.blank? ? val : self
else
self
end
end
end
class NilClass
def or_if_blank?(val)
val
end
end
That way you get it everywhere blank? is defined (Hash, Array, String, Nil,
Numeric)
And the problem with http://gist.github.com/53846 is that I don't think you
can do stuff on regular strings like this:
user_msg = params[:stuff].or_if_blank? 'didnt type anything'
-Ryan
On Wed, Jan 28, 2009 at 1:21 AM, Ryan Angilly <[email protected]> wrote:
> But then it wouldn't work on regular strings right?
>
> So you couldn't do things like:
>
> user_msg = params[:stuff].or_if_blank? 'didnt type anything'
>
>
>
>
> On Wed, Jan 28, 2009 at 1:18 AM, Chris Eppstein
> <[email protected]>wrote:
>
>>
>> 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
-~----------~----~----~----~------~----~------~--~---