# Writing if statement in one line with elsif condition

**URL:** <https://www.ruby-forum.com/t/writing-if-statement-in-one-line-with-elsif-condition/146066>\
**Category:** Ruby\
**Created:** [August 17, 2008, 5:13am UTC](https://www.ruby-forum.com/t/writing-if-statement-in-one-line-with-elsif-condition/146066 "2008-08-17T05:13:19Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![lz-coelho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/8c91f0/32.png) [@lz-coelho](https://www.ruby-forum.com/u/lz-coelho)\
**Post date:** [August 17, 2008, 5:13am UTC](https://www.ruby-forum.com/t/writing-if-statement-in-one-line-with-elsif-condition/146066/1 "2008-08-17T05:13:19Z")

</div>

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 C.  
cel.: (11) 8187-8662  
blog: [rubz.org](http://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”

---

<div class="post-metadata">

**Author:** ![lz-coelho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/8c91f0/32.png) [@lz-coelho](https://www.ruby-forum.com/u/lz-coelho)\
**Post date:** [August 17, 2008, 5:35am UTC](https://www.ruby-forum.com/t/writing-if-statement-in-one-line-with-elsif-condition/146066/2 "2008-08-17T05:35:31Z")

</div>

Luiz Vitor Martinez C. 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.

---

<div class="post-metadata">

**Author:** ![lz-coelho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/8c91f0/32.png) [@lz-coelho](https://www.ruby-forum.com/u/lz-coelho)\
**Post date:** [August 17, 2008, 5:36am UTC](https://www.ruby-forum.com/t/writing-if-statement-in-one-line-with-elsif-condition/146066/3 "2008-08-17T05:36:42Z")

</div>

On Sat, Aug 16, 2008 at 10:09 PM, Luiz Vitor Martinez C.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![lz-coelho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/8c91f0/32.png) [@lz-coelho](https://www.ruby-forum.com/u/lz-coelho)\
**Post date:** [August 17, 2008, 9:04am UTC](https://www.ruby-forum.com/t/writing-if-statement-in-one-line-with-elsif-condition/146066/4 "2008-08-17T09:04:49Z")

</div>

On Sunday 17 August 2008, Luiz Vitor Martinez C. 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

---

<div class="post-metadata">

**Author:** ![lz-coelho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/8c91f0/32.png) [@lz-coelho](https://www.ruby-forum.com/u/lz-coelho)\
**Post date:** [August 17, 2008, 1:10pm UTC](https://www.ruby-forum.com/t/writing-if-statement-in-one-line-with-elsif-condition/146066/5 "2008-08-17T13:10:30Z")

</div>

On 17.08.2008 05:09, Luiz Vitor Martinez C. 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

---

<div class="post-metadata">

**Author:** ![lz-coelho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/8c91f0/32.png) [@lz-coelho](https://www.ruby-forum.com/u/lz-coelho)\
**Post date:** [August 17, 2008, 3:15pm UTC](https://www.ruby-forum.com/t/writing-if-statement-in-one-line-with-elsif-condition/146066/6 "2008-08-17T15:15:50Z")

</div>

> @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…

---

<div class="post-metadata">

**Author:** ![lz-coelho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/8c91f0/32.png) [@lz-coelho](https://www.ruby-forum.com/u/lz-coelho)\
**Post date:** [August 17, 2008, 5:55pm UTC](https://www.ruby-forum.com/t/writing-if-statement-in-one-line-with-elsif-condition/146066/7 "2008-08-17T17:55:49Z")

</div>

Luiz Vitor Martinez C. wrote:

> I really know!

But did any respondent challenge the requirement? Why should any  
statement have  
to fit on one line??

---

<div class="post-metadata">

**Author:** ![lz-coelho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/8c91f0/32.png) [@lz-coelho](https://www.ruby-forum.com/u/lz-coelho)\
**Post date:** [August 18, 2008, 12:20am UTC](https://www.ruby-forum.com/t/writing-if-statement-in-one-line-with-elsif-condition/146066/8 "2008-08-18T00:20:03Z")

</div>

Just for curious. I’m curious when i’m learning something new 😉

Regards,

On Sun, Aug 17, 2008 at 12:51 PM, Phlip [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

–  
Regards,

Luiz Vitor Martinez C.  
cel.: (11) 8187-8662  
blog: [rubz.org](http://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”

---

<div class="post-metadata">

**Author:** ![lz-coelho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/8c91f0/32.png) [@lz-coelho](https://www.ruby-forum.com/u/lz-coelho)\
**Post date:** [August 18, 2008, 12:35am UTC](https://www.ruby-forum.com/t/writing-if-statement-in-one-line-with-elsif-condition/146066/9 "2008-08-18T00:35:51Z")

</div>

Luiz Vitor Martinez C. 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.

---

<div class="post-metadata">

**Author:** ![lz-coelho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/8c91f0/32.png) [@lz-coelho](https://www.ruby-forum.com/u/lz-coelho)\
**Post date:** [August 17, 2008, 4:16pm UTC](https://www.ruby-forum.com/t/writing-if-statement-in-one-line-with-elsif-condition/146066/10 "2008-08-17T16:16:42Z")

</div>

Philp,

I really know!

Thanks,

On Sun, Aug 17, 2008 at 10:11 AM, Phlip [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

–  
Regards,

Luiz Vitor Martinez C.  
cel.: (11) 8187-8662  
blog: [rubz.org](http://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”
