Forum: Ruby writing if statement in one line with elsif condition

045393257ca8795742d87e6b2945f151?d=identicon&s=25 Luiz Vitor Martinez Cardoso (Guest)
on 2008-08-17 05:13
(Received via mailing list)
I'm trying to convert it:

            <% if @recipes.length == (i+1) %>
                <% @id = 0 %>
            <% elsif i.remainder(2) == 1 %>
                <% @id = 1 %>
            <% else %>
                <% @id = 2 %>
            <% end %>

To something like:

            <% if @recipes.length == (i+1) then @id = 0 ; elsif
i.remainder(2) == 1 then @id = 1 ; else @id = 2 end >%

But in my tests the elsif condition is totally ignored, why?

Thanks for you attention!

--
Regards,

Luiz Vitor Martinez Cardoso
cel.: (11) 8187-8662
blog: rubz.org
engineer student at maua.br

"Posso nunca chegar a ser o melhor engenheiro do mundo, mas tenha
certeza de
que eu vou lutar com todas as minhas forças para ser o melhor engenheiro
que
eu puder ser"
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2008-08-17 05:35
(Received via mailing list)
Luiz Vitor Martinez Cardoso wrote:

>  <% if @recipes.length == (i+1) then @id = 0 ; elsif
> i.remainder(2) == 1 then @id = 1 ; else @id = 2 end >%

How about this?

      <% if @recipes.length == (i+1)
           @id = 0
         elsif i.remainder(2) == 1
           @id = 1
         else
           @id = 2
         end %>

I can't see what your specific error is, but that structure will at
least be
easier to read and debug.

Also, any logic you can move to a controller - out of the <% erb %> -
move it
there. Or better, into a model. The view should have the minimum
possible logic.
289cf19aa581c445915c072bf45c5e25?d=identicon&s=25 Todd Benson (Guest)
on 2008-08-17 05:36
(Received via mailing list)
On Sat, Aug 16, 2008 at 10:09 PM, Luiz Vitor Martinez Cardoso
<grabber@gmail.com> wrote:
> To something like:
>
>            <% if @recipes.length == (i+1) then @id = 0 ; elsif
> i.remainder(2) == 1 then @id = 1 ; else @id = 2 end >%
>
> But in my tests the elsif condition is totally ignored, why?

Not sure, but...

if 1==1 then 'a' elsif 'a'=='a' then 'b' else 'c' end
=> "a"

if 1==2 then 'a' elsif 'a'=='b' then 'b' else 'c' end
=> "c"


With a cursory glance, I can only guess that your first condition is
always true.  Maybe somebody could pipe in about ERB if that's a
factor, because I'm not familiar with it.

Todd
C40020a47c6b625af6422b5b1302abaf?d=identicon&s=25 Stefano Crocco (crocco)
on 2008-08-17 09:04
(Received via mailing list)
On Sunday 17 August 2008, Luiz Vitor Martinez Cardoso wrote:
> To something like:
>
>             <% if @recipes.length == (i+1) then @id = 0 ; elsif
> i.remainder(2) == 1 then @id = 1 ; else @id = 2 end >%
>
> But in my tests the elsif condition is totally ignored, why?
>
> Thanks for you attention!

It should work. Look at this:

require 'erb'
puts "Insert a number"
n = gets.to_i
str = '<%= if n%3 == 0 then "divisible by three";elsif n%2==0 then
"divisible by two";end%>'
puts ERB.new(str).result

Entering 3 gives "divisible by 3", while entering 2 gives "divisible by
two".

Stefano
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2008-08-17 13:10
(Received via mailing list)
On 17.08.2008 05:09, Luiz Vitor Martinez Cardoso wrote:
> To something like:
>
>             <% if @recipes.length == (i+1) then @id = 0 ; elsif
> i.remainder(2) == 1 then @id = 1 ; else @id = 2 end >%
>
> But in my tests the elsif condition is totally ignored, why?
>
> Thanks for you attention!

Agreeing with the others: it's probably your expectation about the first
condition which is wrong.

Btw, here's another way to do it

@id = case when @recipes.length == i + 1: 0; when i.remainder(2) == 1:
1; else 2 end

Although I admit that I'd rather have it on several lines

@id = case
   when @recipes.length == i + 1: 0
   when i.remainder(2) == 1: 1
   else 2
end

Kind regards

  robert
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2008-08-17 15:15
(Received via mailing list)
> @id = case when @recipes.length == i + 1: 0; when i.remainder(2) == 1:
> 1; else 2 end
>
> Although I admit that I'd rather have it on several lines

I suspect the original posted might not know you can write a multi-line
<% erb %> block! Some sample code puts a different <% %> on each line,
like the original post did...
045393257ca8795742d87e6b2945f151?d=identicon&s=25 Luiz Vitor Martinez Cardoso (Guest)
on 2008-08-17 16:16
(Received via mailing list)
Philp,

I really know!

Thanks,

On Sun, Aug 17, 2008 at 10:11 AM, Phlip <phlip2005@gmail.com> wrote:

>
>


--
Regards,

Luiz Vitor Martinez Cardoso
cel.: (11) 8187-8662
blog: rubz.org
engineer student at maua.br

"Posso nunca chegar a ser o melhor engenheiro do mundo, mas tenha
certeza de
que eu vou lutar com todas as minhas forças para ser o melhor engenheiro
que
eu puder ser"
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2008-08-17 17:55
(Received via mailing list)
Luiz Vitor Martinez Cardoso wrote:

> I really know!

But did any respondent challenge the requirement? Why should any
statement have
to fit on one line??
045393257ca8795742d87e6b2945f151?d=identicon&s=25 Luiz Vitor Martinez Cardoso (Guest)
on 2008-08-18 00:20
(Received via mailing list)
Just for curious. I'm curious when i'm learning something new ;)

Regards,

On Sun, Aug 17, 2008 at 12:51 PM, Phlip <phlip2005@gmail.com> wrote:

>
>


--
Regards,

Luiz Vitor Martinez Cardoso
cel.: (11) 8187-8662
blog: rubz.org
engineer student at maua.br

"Posso nunca chegar a ser o melhor engenheiro do mundo, mas tenha
certeza de
que eu vou lutar com todas as minhas forças para ser o melhor engenheiro
que
eu puder ser"
5316d692cc603c3ad2df0f03869bb96b?d=identicon&s=25 Michael Morin (Guest)
on 2008-08-18 00:35
(Received via mailing list)
Luiz Vitor Martinez Cardoso wrote:
> To something like:
>
>             <% if @recipes.length == (i+1) then @id = 0 ; elsif
> i.remainder(2) == 1 then @id = 1 ; else @id = 2 end >%
>
> But in my tests the elsif condition is totally ignored, why?
>
> Thanks for you attention!
>

Ideally, even this rather trivial code is best left outside of ERB
files.  Hide this away in a function and it can be as clean and
multi-liney as you want and doesn't muck up your ERB files.  This is
doubleplus true if this code is used more than once.  Cramming the
entire if statement onto line just makes it less readable.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.