[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

new to ruby - please help w/ code structure?

Corey

5/6/2005 11:53:00 PM


I'd like to make the following code more terse... i.e. more expressive w/ less
lines:

if free || os
if line =~ new_re
cvs(host)
host, free, os = nil
next
end
end

I'm looking for something more along the lines of:

( line =~ new_re && do { cvs(host); host, free, os = nil; next } )
if free || os

But of course that's giving me plenty of errors...

What would the ruby idiom for the above be?

( I'm coming from perl... so I'm still thinking in perl unfortunately )

Many thanks!







4 Answers

Zach Dennis

5/7/2005 12:35:00 AM

0

Corey wrote:
> I'd like to make the following code more terse... i.e. more expressive w/ less
> lines:
>
> if free || os
> if line =~ new_re
> cvs(host)
> host, free, os = nil
> next
> end
> end

I dont know what you're doing, but to take exactly what you typed:

if free||os and line=~new_re
cvs host ; host,free,os=nil ; end

OR

(cvs host ; host,free,os=nil ; next) if free||os and line=~re

Zach


Corey

5/7/2005 12:56:00 AM

0

On Friday 06 May 2005 05:34 pm, Zach Dennis wrote:
> I dont know what you're doing,
>

Heh - that's ok, neither do I!

<grin>


> but to take exactly what you typed:
>
> if free||os and line=~new_re
> cvs host ; host,free,os=nil ; end
>
> OR
>
> (cvs host ; host,free,os=nil ; next) if free||os and line=~re
>

Perfect -- many thanks!


Beers,

Corey





James Gray

5/7/2005 2:46:00 AM

0

On May 6, 2005, at 7:55 PM, Corey wrote:

>> but to take exactly what you typed:
>>
>> if free||os and line=~new_re
>> cvs host ; host,free,os=nil ; end
>>
>> OR
>>
>> (cvs host ; host,free,os=nil ; next) if free||os and line=~re
>>
>>
>
> Perfect -- many thanks!

Funny, I was thinking just the opposite. :D Us Ruby guys love
pretty code.

That's you're Perl self talking, trust me. Let it die. You won't
miss it, I promise.

Try this question on for size: Give me one single advantage to the
above changes? Shorter? Please. Harder to code (that's why you
asked), harder to understand, harder to change, etc. For what? You
dropped a few lines.

Ruby is truly a beautiful language. Give it a chance to work its
magic on you and don't be surprised if you eventually find yourself
saying, "No, that's too ugly. I can do better..."

Just a small piece of advice, from one ex-Perl junkie to another. No
offense intended. :)

James Edward Gray II



Corey

5/7/2005 3:28:00 AM

0

On Friday 06 May 2005 07:46 pm, James Edward Gray II wrote:
<snippage>
> Just a small piece of advice, from one ex-Perl junkie to another. No
> offense intended. :)
>

You speak the truth.

I was coding ruby exactly the way I would have coded in perl... I think mainly
because this was precisely the sort of script that perl excels at: a quick
hackish, throw-away thing to parse a file. I actualy started it in perl,
then figured I'd try in ruby just as a quick immersion excercise... learn
some syntax and whatnot. I got imipatient because I really needed this thing
super quick... so, no harm done - when I have more time, I definitely intend
to learn the actual ruby idioms; I totally agree with your excellent points.

Beers!

Corey