[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

How can I make this if/else more succinct?

eggie5

8/25/2007 2:35:00 AM

How can I make this if/else more succinct?

Video.find(:all).each do |video|
if video._3gp
ext=".3gp"
elsif video._3g2
ext=".3g2"
elsif video.mp4
ext=".mp4"
end

12 Answers

eggie5

8/25/2007 2:45:00 AM

0

Actually how can this be more succinct?

Video.find(:all).each do |video|
if video._3gp ==1
ext=".3gp"
elsif video._3g2==1
ext=".3g2"
elsif video.mp4==1
ext=".mp4"
end
expected.push "#{self.id}_#{video.id}#{ext}"
end

Logan Capaldo

8/25/2007 3:01:00 AM

0

On 8/24/07, eggie5 <eggie5@gmail.com> wrote:
> Actually how can this be more succinct?
>
> Video.find(:all).each do |video|
ext = %w(_3gp _3g2 mp4).find { |m| video.send(m) == 1
}.to_s.sub(/\A_?(.+)\z/, '.\\1')
> expected.push "#{self.id}_#{video.id}#{ext}"
> end
>
>
>

eggie5

8/25/2007 4:41:00 AM

0

On Aug 24, 8:01 pm, "Logan Capaldo" <logancapa...@gmail.com> wrote:
> On 8/24/07, eggie5 <egg...@gmail.com> wrote:> Actually how can this be more succinct?
>
> > Video.find(:all).each do |video|
>
> ext = %w(_3gp _3g2 mp4).find { |m| video.send(m) == 1
>
> }.to_s.sub(/\A_?(.+)\z/, '.\\1')
> > expected.push "#{self.id}_#{video.id}#{ext}"
> > end

this works great, thanks.

Ari Brown

8/25/2007 4:47:00 PM

0


On Aug 24, 2007, at 10:45 PM, eggie5 wrote:

> Actually how can this be more succinct?
>
> Video.find(:all).each do |video|
> if video._3gp ==1
> ext=".3gp"
> elsif video._3g2==1
> ext=".3g2"
> elsif video.mp4==1
> ext=".mp4"
> end
> expected.push "#{self.id}_#{video.id}#{ext}"
> end

You can use case-when:

Video.find(:all).each do |video|
case 1
when video._3gp : ext = ".3gp"
when video._3gp2 : ext = ".3gp2"
when video.mp4 : ext = ".mp4"
end
expected.push "#{self.id}_#{video.id}#{ext}"
end


~ Ari
English is like a pseudo-random number generator - there are a
bajillion rules to it, but nobody cares.


dblack

8/25/2007 9:27:00 PM

0

Jacob Dunphy

8/25/2007 10:08:00 PM

0

It may not be more succinct, but it reads better to me.

class Video

def ext
if self._3gp ==1
".3gp"
elsif self._3g2==1
".3g2"
elsif self.mp4==1
".mp4"
end
end
end

expected = Video.find(:all).collect do |video|
"#{self.id}_#{video.id}#{video.ext}"
end


On 8/24/07, eggie5 <eggie5@gmail.com> wrote:
> How can I make this if/else more succinct?
>
> Video.find(:all).each do |video|
> if video._3gp
> ext=".3gp"
> elsif video._3g2
> ext=".3g2"
> elsif video.mp4
> ext=".mp4"
> end
>
>
>


--
Jacob Dunphy
http://dunphyd...
626.318.2566

Stefan Rusterholz

8/25/2007 10:28:00 PM

0

eggie5 wrote:
> How can I make this if/else more succinct?
>
> Video.find(:all).each do |video|
> if video._3gp
> ext=".3gp"
> elsif video._3g2
> ext=".3g2"
> elsif video.mp4
> ext=".mp4"
> end

If I see that correctly you have a database with a table with the 3
fields "_3gp", "_3g2" and "mp4".
If that's incorrect, don't bother to read on.
If it is correct: why? It seems much saner to have a field 'type' with
an enum '3gp','3g2','mp4' NOT NULL.
That way you can create your extension simply by prefixing a dot.

Regards
Stefan
--
Posted via http://www.ruby-....

Esmail

8/26/2007 11:07:00 PM

0

Logan Capaldo wrote:
> On 8/24/07, eggie5 <eggie5@gmail.com> wrote:
>> Actually how can this be more succinct?
>>
>> Video.find(:all).each do |video|
> ext = %w(_3gp _3g2 mp4).find { |m| video.send(m) == 1
> }.to_s.sub(/\A_?(.+)\z/, '.\\1')
>> expected.push "#{self.id}_#{video.id}#{ext}"
>> end

this is not meant as a flame, but this reminds me of the
"write only" code I used to see when I programmed in C.

If given the choice between the two, I would prefer the
original as it's easily comprehensible and to modify.

One of the things I like about Python and Ruby is that they
encourage/enable clean readable code, so I find the above
going against the design philosophy of the language.

Comments? Please, I am not looking for an argument here.

dblack

8/26/2007 11:30:00 PM

0

Esmail

8/27/2007 12:05:00 AM

0

dblack@wobblini.net wrote:
> Hi --
>
> On Mon, 27 Aug 2007, Esmail wrote:
>
<..>
>> Comments? Please, I am not looking for an argument here.
>
> The original question was how to make it more succinct, not more
> elegant or transparent. As it happens, I don't think there's anything
> particularly unclean or unreadable about the code above, though I've
> pretty much given up on "readable" as a meaningful term.

:-)

> Another point is that while Ruby certain does (in my view) provide
> favorable conditions for writing nice-looking code, it's important not
> to be *too* skittish about the use of constructs that can't always be
> picked up at a glance. Some Ruby constructs are easier to read, and
> for different people, than others. It was never part of the Ruby
> contract, so to speak, that no one would have to make an effort to
> learn how to read Ruby.

Good points .. though in general I will favor "readability" (whatever
that exactly means to everyone) over "cleverness" .. after all people
end up reading/comprehending and possibly modifying the code, so it's
good to help them as much as possible. Of course, as you say, this
doesn't mean no effort should be made to know the language and its
idiomatic expressions.

Esmail