[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

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

Joe Ruby

9/8/2006 8:50:00 PM

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-....

13 Answers

matthew.moss.coder

9/8/2006 8:56:00 PM

0

> 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

Joel VanderWerf

9/8/2006 9:54:00 PM

0

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

Ara.T.Howard

9/8/2006 10:11:00 PM

0

Ara.T.Howard

9/9/2006 2:57:00 PM

0

Logan Capaldo

9/9/2006 8:21:00 PM

0


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

> ara.t.howard@noaa.gov 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
>
>


Ken Bloom

9/10/2006 2:41:00 AM

0

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...

Farrel Lifson

9/10/2006 8:05:00 AM

0

On 08/09/06, Joe Ruby <joeat303@yahoo.com> 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-....
>
>

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

Austin Ziegler

9/11/2006 9:59:00 PM

0

On 9/9/06, Marshall T. Vandegrift <llasram@gmail.com> 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 * halostatue@gmail.com * http://www.halo...
* austin@halostatue.ca * http://www.halo...feed/
* austin@zieglers.ca

Barry Margolin

10/30/2011 5:26:00 PM

0

In article <se80o8xrfb.ln2@app-01.ezprovider.com>,
"Ubiquitous" <weberm@polaris.net> wrote:

> What's with all these questions about smoking this morning?

Are there any questions about smoking other than this one and the one
about the Cain ad?

I suspect the Cain ad may have triggered him noticing the reality show
smoking.

--
Barry Margolin
Arlington, MA

Paul Morabito

11/1/2011 12:37:00 PM

0

On Sun, 30 Oct 2011 13:25:31 -0400, Barry Margolin
<barmar@alum.mit.edu> wrote:

>In article <se80o8xrfb.ln2@app-01.ezprovider.com>,
> "Ubiquitous" <weberm@polaris.net> wrote:
>
>> What's with all these questions about smoking this morning?
>
>Are there any questions about smoking other than this one and the one
>about the Cain ad?
>
>I suspect the Cain ad may have triggered him noticing the reality show
>smoking.

I am not tea party by any means, but didn't Obama smoke until very
recently, if he actually quit at all?