Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Combine @item.foo.nil? || @item.foo.empty? ?

Reply
Thread Tools

Combine @item.foo.nil? || @item.foo.empty? ?

 
 
Joe Ruby
Guest
Posts: n/a
 
      09-08-2006
I have code like this in my (Markaby) templates:

tr do
td @item.foo
end unless (@item.foo.nil? || @item.foo.empty?)

Is there a way to combine these two OR clauses into one?

tr do
td @item.foo
end unless @item.foo.nil_or_empty?

Thanks,
Joe

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Matthew Moss
Guest
Posts: n/a
 
      09-08-2006
> tr do
> td @item.foo
> end unless (@item.foo.nil? || @item.foo.empty?)
>
> Is there a way to combine these two OR clauses into one?



Define a method on the class of foo?

class Foo
def nil_or_empty?
return nil? || empty?
end
end

 
Reply With Quote
 
 
 
 
Joel VanderWerf
Guest
Posts: n/a
 
      09-08-2006
Matthew Moss wrote:
>> tr do
>> td @item.foo
>> end unless (@item.foo.nil? || @item.foo.empty?)
>>
>> Is there a way to combine these two OR clauses into one?

>
>
> Define a method on the class of foo?
>
> class Foo

^^^
Object
> def nil_or_empty?
> return nil? || empty?
> end
> end


Otherwise it won't be defined when foo==nil.

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

 
Reply With Quote
 
ara.t.howard@noaa.gov
Guest
Posts: n/a
 
      09-08-2006
On Sat, 9 Sep 2006, Joe Ruby wrote:

> I have code like this in my (Markaby) templates:
>
> tr do
> td @item.foo
> end unless (@item.foo.nil? || @item.foo.empty?)
>
> Is there a way to combine these two OR clauses into one?
>
> tr do
> td @item.foo
> end unless @item.foo.nil_or_empty?
>
> Thanks,
> Joe


tr{ tr @item.foo } unless @item.foo.to_s.empty?

-a
--
what science finds to be nonexistent, we must accept as nonexistent; but what
science merely does not find is a completely different matter... it is quite
clear that there are many, many mysterious things.
- h.h. the 14th dalai lama

 
Reply With Quote
 
ara.t.howard@noaa.gov
Guest
Posts: n/a
 
      09-09-2006
On Sat, 9 Sep 2006, Marshall T. Vandegrift wrote:

> writes:
>
>> tr{ tr @item.foo } unless @item.foo.to_s.empty?

>
> It makes sense to me that
>
> object.to_s.empty? # => true
>
> should imply
>
> object.empty? # => true
>
> So why not just do
>
> class NilClass
> def empty?; true; end
> end
>
> ?
>
> -Marshall




def deep_in_some_other_unrelated_code
begin
should_always_be_empty = buggy_method

abort unless should_always_be_empty.empty? # OOPS!

transfer_four_million_euros_to_swiss_account
rescue
hours_of_debugging?
ensure
modify_builtins_with_care!
end
end



-a
--
what science finds to be nonexistent, we must accept as nonexistent; but what
science merely does not find is a completely different matter... it is quite
clear that there are many, many mysterious things.
- h.h. the 14th dalai lama

 
Reply With Quote
 
Logan Capaldo
Guest
Posts: n/a
 
      09-09-2006

On Sep 9, 2006, at 10:37 AM, Marshall T. Vandegrift wrote:

> writes:
>
>> tr{ tr @item.foo } unless @item.foo.to_s.empty?

>
> It makes sense to me that
>
> object.to_s.empty? # => true
>
> should imply
>
> object.empty? # => true
>

Doesn't to me. nil is not a container, and it should not be treated
as one. A string is a container (contains a sequence of characters)
and can therefore be empty. The fact that one, arbitrary string
representation of nil happens to be the empty string doesn't mean
that nil is empty. (This is also why I disklike rails's #blank?, but
at least it's named something different.)

> So why not just do
>
> class NilClass
> def empty?; true; end
> end
>
> ?
>
> -Marshall
>
>



 
Reply With Quote
 
Ken Bloom
Guest
Posts: n/a
 
      09-10-2006
On Sat, 09 Sep 2006 06:53:59 +0900, Joel VanderWerf wrote:

> Matthew Moss wrote:
>>> tr do
>>> td @item.foo
>>> end unless (@item.foo.nil? || @item.foo.empty?)
>>>
>>> Is there a way to combine these two OR clauses into one?

>>
>>
>> Define a method on the class of foo?
>>
>> class Foo

> ^^^
> Object
>> def nil_or_empty?
>> return nil? || empty?
>> end
>> end

>
> Otherwise it won't be defined when foo==nil.
>


So define as follows:

class NilClass
def nil_or_empty?
true
end
end

--
Ken Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/
 
Reply With Quote
 
Farrel Lifson
Guest
Posts: n/a
 
      09-10-2006
On 08/09/06, Joe Ruby <> wrote:
> I have code like this in my (Markaby) templates:
>
> tr do
> td @item.foo
> end unless (@item.foo.nil? || @item.foo.empty?)
>
> Is there a way to combine these two OR clauses into one?
>
> tr do
> td @item.foo
> end unless @item.foo.nil_or_empty?
>
> Thanks,
> Joe
>
> --
> Posted via http://www.ruby-forum.com/.
>
>


tr do
td @item.foo
end unless Array(@item).empty?

 
Reply With Quote
 
Austin Ziegler
Guest
Posts: n/a
 
      09-11-2006
On 9/9/06, Marshall T. Vandegrift <> wrote:
> class NilClass
> def empty?; true; end
> end


Because nil isn't empty.

It isn't a container. It's not an array, hash, or string.

Rails has a #blank? method -- which I've used this weekend -- but
nil.empty? is a false question.

-austin
--
Austin Ziegler * * http://www.halostatue.ca/
* * http://www.halostatue.ca/feed/
*

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Combine and Decode multipart messages gandalf Firefox 2 09-13-2005 08:18 PM
Combine and Decode YENC Agent777 Firefox 7 05-14-2005 11:22 PM
combine and decode? Gary Firefox 1 01-07-2005 06:53 PM
Combine-and-Decode multipart news posts? William W. Plummer Firefox 0 07-03-2004 12:37 AM
Combine and Decode? Chris Firefox 0 11-28-2003 04:40 PM



Advertisments