[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

better idiom for title on an optional list?

slothbear

11/29/2007 5:22:00 PM

I'm still learning Ruby, and most of the time I can find a beautiful
Ruby idiom that collapses two or more statements into one easy-to-read
(or learn) idiom, like h[key] ||= 0. My current project has a lot of
places where it emits lists if they contain any items, much like this:

unless items.empty?
puts "items\n-----"
items.each { |item| puts item }
end

My header code and list code is more complicated, and sometimes there
are lists within lists, and it all just starts to look ugly, even
split out into smaller methods. It would seem more Ruby-like if I
could fold the header into the iterator somehow -- rather than what I
have now that feels like it tests the Array for items twice. Or maybe
I'm trying too hard to make it terse?
12 Answers

James Gray

11/29/2007 5:34:00 PM

0

On Nov 29, 2007, at 11:25 AM, slothbear wrote:

> I'm still learning Ruby, and most of the time I can find a beautiful
> Ruby idiom that collapses two or more statements into one easy-to-read
> (or learn) idiom, like h[key] ||= 0. My current project has a lot of
> places where it emits lists if they contain any items, much like this:
>
> unless items.empty?
> puts "items\n-----"
> items.each { |item| puts item }
> end

puts "items",
"-----",
items unless items.empty?

I don't guess that's much better.

It does feel like it should be a separate method to me.

James Edward Gray II

ara.t.howard

11/29/2007 5:45:00 PM

0


On Nov 29, 2007, at 10:25 AM, slothbear wrote:

> I'm still learning Ruby, and most of the time I can find a beautiful
> Ruby idiom that collapses two or more statements into one easy-to-read
> (or learn) idiom, like h[key] ||= 0. My current project has a lot of
> places where it emits lists if they contain any items, much like this:
>
> unless items.empty?
> puts "items\n-----"
> items.each { |item| puts item }
> end
>
> My header code and list code is more complicated, and sometimes there
> are lists within lists, and it all just starts to look ugly, even
> split out into smaller methods. It would seem more Ruby-like if I
> could fold the header into the iterator somehow -- rather than what I
> have now that feels like it tests the Array for items twice. Or maybe
> I'm trying too hard to make it terse?
>

1)

puts "list", "----", list unless list.empty?

2) refactor you code so that this is ok

require 'yaml'

y 'list' => list

this will print and empty list. personally i think this is best
since analysis of output is very hard when the schema keeps changing

3) same as above, but with test

y 'list' => list unless list.empty?

4) dry it up

def report label, list
puts label, '---', list unless list.empty?
end

report 'foo', foo

5) create a list class and use it

class List < ::Array
def initialize label
@label = label
end

def to_s
...
end

unfortunately this gets icky because ruby handles any subclass of
array specically when given as an arg to puts...


regards.
a @ http://codeforp...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




slothbear

12/4/2007 9:27:00 PM

0

Thanks for the ideas. I ended up creating a separate method that
accepts a label and list as arguments. It looks pretty good and feels
pretty natural. Success! my thanks again.

sb

suzeeq

11/5/2013 1:11:00 AM

0

wolfagain wrote:
> On Monday, November 4, 2013 1:17:31 PM UTC-8, Bill Steele wrote:
>> In article <1b5b4831-8188-4fc6-bcf2-e4e7760a6308@googlegroups.com>,
>>
>> wolfagain <wolf@provide.net> wrote:
>>
>>
>>
>>> search for the next "Ratings Game Changer!"
>>
>>
>> David s hardly responsible for the language press release writers use,
>>
>> any more than you are for the silliness of the right-wing stuff you
>>
>> quote. At least David is on topic.
>
> The little fella IS Responsible for HAWKING TV shows he's never seen...

Idiot. He's informing the rest of us about future shows or changes to
current ones. One doesn't have to have seen them all to pass on
information about them.

wolfagain

11/5/2013 5:22:00 AM

0

On Monday, November 4, 2013 5:10:44 PM UTC-8, suzeeq wrote:
> wolfagain wrote:
>
> > On Monday, November 4, 2013 1:17:31 PM UTC-8, Bill Steele wrote:
>
> >> In article <1b5b4831-8188-4fc6-bcf2-e4e7760a6308@googlegroups.com>,
>
> >>
>
> >> wolfagain <wolf@provide.net> wrote:
>
> >>
>
> >>
>
> >>
>
> >>> search for the next "Ratings Game Changer!"
>
> >>
>
> >>
>
> >> David s hardly responsible for the language press release writers use,
>
> >>
>
> >> any more than you are for the silliness of the right-wing stuff you
>
> >>
>
> >> quote. At least David is on topic.
>
> >
>
> > The little fella IS Responsible for HAWKING TV shows he's never seen...
>
>
>
> Idiot. He's informing the rest of us about future shows or changes to
>
> current ones. One doesn't have to have seen them all to pass on
>
> information about them.

DUMB ASS!! remember when PBS aired "Siskel & Ebert" for many years...how many would have watched if they reviewed films they had never seen...and just Parroted the publicity dept releases? If I was your daddy...you come across here as a little spoiled brat who has no respect for your superiors and elders...and needs a spanking!

suzeeq

11/5/2013 2:40:00 PM

0

wolfagain wrote:
> On Monday, November 4, 2013 5:10:44 PM UTC-8, suzeeq wrote:
>> wolfagain wrote:
>>
>>> On Monday, November 4, 2013 1:17:31 PM UTC-8, Bill Steele wrote:
>>>> In article <1b5b4831-8188-4fc6-bcf2-e4e7760a6308@googlegroups.com>,
>>>> wolfagain <wolf@provide.net> wrote:
>>>>> search for the next "Ratings Game Changer!"
>>>> David s hardly responsible for the language press release writers use,
>>>> any more than you are for the silliness of the right-wing stuff you
>>>> quote. At least David is on topic.
>>> The little fella IS Responsible for HAWKING TV shows he's never seen...
>>
>>
>> Idiot. He's informing the rest of us about future shows or changes to
>>
>> current ones. One doesn't have to have seen them all to pass on
>>
>> information about them.
>
> DUMB ASS!! remember when PBS aired "Siskel & Ebert" for many years...how many would have watched if they reviewed films they had never seen...and just Parroted the publicity dept releases? If I was your daddy...you come across here as a little spoiled brat who has no respect for your superiors and elders...and needs a spanking!

Big differenece, he's not writing a review of TV shows. And I bet I'm
older than you. Right wing idiot.

Bill Steele

11/5/2013 7:33:00 PM

0

In article <l5avv6$b5l$1@dont-email.me>, suzeeq <suzee@imbris.com>
wrote:

> wolfagain wrote:
> > On Monday, November 4, 2013 5:10:44 PM UTC-8, suzeeq wrote:
> >> wolfagain wrote:
> >>
> >>> On Monday, November 4, 2013 1:17:31 PM UTC-8, Bill Steele wrote:
> >>>> In article <1b5b4831-8188-4fc6-bcf2-e4e7760a6308@googlegroups.com>,
> >>>> wolfagain <wolf@provide.net> wrote:
> >>>>> search for the next "Ratings Game Changer!"
> >>>> David s hardly responsible for the language press release writers use,
> >>>> any more than you are for the silliness of the right-wing stuff you
> >>>> quote. At least David is on topic.
> >>> The little fella IS Responsible for HAWKING TV shows he's never seen...
> >>
> >>
> >> Idiot. He's informing the rest of us about future shows or changes to
> >>
> >> current ones. One doesn't have to have seen them all to pass on
> >>
> >> information about them.
> >
> > DUMB ASS!! remember when PBS aired "Siskel & Ebert" for many years...how
> > many would have watched if they reviewed films they had never seen...and
> > just Parroted the publicity dept releases? If I was your daddy...you come
> > across here as a little spoiled brat who has no respect for your superiors
> > and elders...and needs a spanking!
>
> Big differenece, he's not writing a review of TV shows. And I bet I'm
> older than you. Right wing idiot.

Wolfie is just irritated because David's posts remind people that this
newsgroup is about television, not Obama.

And I bet I'm older than both of you.

First movie I ever saw in a theater: Chapter One of Flash Gordon
Conquers the Universe.

Try these:

"Hello, Duffy's Tavern, where the ______________________"

"The way you walk is thorny, through no _______________"

Who was often menaced by a lewolower?

Dano

11/5/2013 8:23:00 PM

0

"Bill Steele" wrote in message
news:ws21-AE6460.14325005112013@70-3-168-216.pools.spcsdns.net...

In article <l5avv6$b5l$1@dont-email.me>, suzeeq <suzee@imbris.com>
wrote:

> wolfagain wrote:
> > On Monday, November 4, 2013 5:10:44 PM UTC-8, suzeeq wrote:
> >> wolfagain wrote:
> >>
> >>> On Monday, November 4, 2013 1:17:31 PM UTC-8, Bill Steele wrote:
> >>>> In article <1b5b4831-8188-4fc6-bcf2-e4e7760a6308@googlegroups.com>,
> >>>> wolfagain <wolf@provide.net> wrote:
> >>>>> search for the next "Ratings Game Changer!"
> >>>> David s hardly responsible for the language press release writers
> >>>> use,
> >>>> any more than you are for the silliness of the right-wing stuff you
> >>>> quote. At least David is on topic.
> >>> The little fella IS Responsible for HAWKING TV shows he's never
> >>> seen...
> >>
> >>
> >> Idiot. He's informing the rest of us about future shows or changes to
> >>
> >> current ones. One doesn't have to have seen them all to pass on
> >>
> >> information about them.
> >
> > DUMB ASS!! remember when PBS aired "Siskel & Ebert" for many years...how
> > many would have watched if they reviewed films they had never seen...and
> > just Parroted the publicity dept releases? If I was your daddy...you
> > come
> > across here as a little spoiled brat who has no respect for your
> > superiors
> > and elders...and needs a spanking!
>
> Big differenece, he's not writing a review of TV shows. And I bet I'm
> older than you. Right wing idiot.

Wolfie is just irritated because David's posts remind people that this
newsgroup is about television, not Obama.

And I bet I'm older than both of you.

First movie I ever saw in a theater: Chapter One of Flash Gordon
Conquers the Universe.

Try these:

"Hello, Duffy's Tavern, where the ______________________"

"The way you walk is thorny, through no _______________"

Who was often menaced by a lewolower?

=========================================================

I thought you wished to remind everyone that this is a TV newsgroup? And
here you go talking about movies. For shame sir!

suzeeq

11/6/2013 1:43:00 AM

0

Bill Steele wrote:
> In article <l5avv6$b5l$1@dont-email.me>, suzeeq <suzee@imbris.com>
> wrote:
>
>> wolfagain wrote:
>>> On Monday, November 4, 2013 5:10:44 PM UTC-8, suzeeq wrote:
>>>> wolfagain wrote:
>>>>
>>>>> On Monday, November 4, 2013 1:17:31 PM UTC-8, Bill Steele wrote:
>>>>>> In article <1b5b4831-8188-4fc6-bcf2-e4e7760a6308@googlegroups.com>,
>>>>>> wolfagain <wolf@provide.net> wrote:
>>>>>>> search for the next "Ratings Game Changer!"
>>>>>> David s hardly responsible for the language press release writers use,
>>>>>> any more than you are for the silliness of the right-wing stuff you
>>>>>> quote. At least David is on topic.
>>>>> The little fella IS Responsible for HAWKING TV shows he's never seen...
>>>>
>>>> Idiot. He's informing the rest of us about future shows or changes to
>>>>
>>>> current ones. One doesn't have to have seen them all to pass on
>>>>
>>>> information about them.
>>> DUMB ASS!! remember when PBS aired "Siskel & Ebert" for many years...how
>>> many would have watched if they reviewed films they had never seen...and
>>> just Parroted the publicity dept releases? If I was your daddy...you come
>>> across here as a little spoiled brat who has no respect for your superiors
>>> and elders...and needs a spanking!
>> Big differenece, he's not writing a review of TV shows. And I bet I'm
>> older than you. Right wing idiot.
>
> Wolfie is just irritated because David's posts remind people that this
> newsgroup is about television, not Obama.
>
> And I bet I'm older than both of you.
>
> First movie I ever saw in a theater: Chapter One of Flash Gordon
> Conquers the Universe.

I remember seeing White Christmas/Holiday Inn when I was about 4, and
also the King and I, but that was probably a few years later. We went
20some miles to the next town to see a movie, so it was a big deal.

> Try these:
>
> "Hello, Duffy's Tavern, where the ______________________"

Vaguely remember this, not the rest of it.

>
> "The way you walk is thorny, through no _______________"
>
> Who was often menaced by a lewolower?

Neither of these.

suzeeq

11/6/2013 1:45:00 AM

0

Dano wrote:
> "Bill Steele" wrote in message
> news:ws21-AE6460.14325005112013@70-3-168-216.pools.spcsdns.net...
>
> In article <l5avv6$b5l$1@dont-email.me>, suzeeq <suzee@imbris.com>
> wrote:
>
>> wolfagain wrote:
>>> On Monday, November 4, 2013 5:10:44 PM UTC-8, suzeeq wrote:
>>>> wolfagain wrote:
>>>>
>>>>> On Monday, November 4, 2013 1:17:31 PM UTC-8, Bill Steele wrote:
>>>>>> In article <1b5b4831-8188-4fc6-bcf2-e4e7760a6308@googlegroups.com>,
>>>>>> wolfagain <wolf@provide.net> wrote:
>>>>>>> search for the next "Ratings Game Changer!"
>>>>>> David s hardly responsible for the language press release writers
>>>>>> use,
>>>>>> any more than you are for the silliness of the right-wing stuff you
>>>>>> quote. At least David is on topic.
>>>>> The little fella IS Responsible for HAWKING TV shows he's never
>>>>> seen...
>>>>
>>>> Idiot. He's informing the rest of us about future shows or changes to
>>>>
>>>> current ones. One doesn't have to have seen them all to pass on
>>>>
>>>> information about them.
>>> DUMB ASS!! remember when PBS aired "Siskel & Ebert" for many years...how
>>> many would have watched if they reviewed films they had never seen...and
>>> just Parroted the publicity dept releases? If I was your daddy...you
>>> come
>>> across here as a little spoiled brat who has no respect for your
>>> superiors
>>> and elders...and needs a spanking!
>> Big differenece, he's not writing a review of TV shows. And I bet I'm
>> older than you. Right wing idiot.
>
> Wolfie is just irritated because David's posts remind people that this
> newsgroup is about television, not Obama.
>
> And I bet I'm older than both of you.
>
> First movie I ever saw in a theater: Chapter One of Flash Gordon
> Conquers the Universe.
>
> Try these:
>
> "Hello, Duffy's Tavern, where the ______________________"
>
> "The way you walk is thorny, through no _______________"
>
> Who was often menaced by a lewolower?
>
> =========================================================
>
> I thought you wished to remind everyone that this is a TV newsgroup? And
> here you go talking about movies. For shame sir!

Okay, old TV shows... Probably not any before the late 50s. We only got
limited transmission from the Big City about 70 miles away, 2 networks
and some of the 3rd network shows split between them. I do remember
Walter Cronkite and 15 minute network news. Then 15 minutes of local news.