# If else statement problem

**URL:** <https://www.ruby-forum.com/t/if-else-statement-problem/118297>\
**Category:** Rails\
**Created:** [October 25, 2007, 1:16pm UTC](https://www.ruby-forum.com/t/if-else-statement-problem/118297 "2007-10-25T13:16:58Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ratschi85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/77aa72/32.png) [@ratschi85](https://www.ruby-forum.com/u/ratschi85)\
**Post date:** [October 25, 2007, 1:16pm UTC](https://www.ruby-forum.com/t/if-else-statement-problem/118297/1 "2007-10-25T13:16:58Z")

</div>

Hey,

I’m trying to do the following below:

if @logos == nil  
@image =  
Magick::ImageList.new("/home/rajeev/Desktop/logo/logo/public/logos/0000/0001/28.jpg")  
@image = @image.write("/home/rajeev/Desktop/test.jpg")  
@logos = Magick::ImageList.new("/home/rajeev/Desktop/test.jpg")  
@logos = @logos.rotate(90)  
@logos = @logos.write("/home/rajeev/Desktop/test.jpg")

else  
@logos = Magick::ImageList.new("/home/rajeev/Desktop/test.jpg")  
@logos = @logos.rotate(90)  
@logos = @logos.write("/home/rajeev/Desktop/test.jpg")  
end

I’m getting an image then creating a copy of it on the desktop. This is  
then rotated and saved in the same location. So for the else statement i  
want to say if the image is already on the desktop location then i just  
want to rotate it again. Hope this makes sense, but the if…else block  
doesn’t seem to work.

Would appreciate any help.

Cheers

---

<div class="post-metadata">

**Author:** ![ratschi85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/77aa72/32.png) [@ratschi85](https://www.ruby-forum.com/u/ratschi85)\
**Post date:** [October 25, 2007, 1:34pm UTC](https://www.ruby-forum.com/t/if-else-statement-problem/118297/2 "2007-10-25T13:34:20Z")

</div>

Rajeev K. wrote:

> Hey,
> 
> I’m trying to do the following below:
> 
> if @logos == nil  
> @image =  
> Magick::ImageList.new("/home/rajeev/Desktop/logo/logo/public/logos/0000/0001/28.jpg")  
> @image = @image.write("/home/rajeev/Desktop/test.jpg")  
> @logos = Magick::ImageList.new("/home/rajeev/Desktop/test.jpg")  
> @logos = @logos.rotate(90)  
> @logos = @logos.write("/home/rajeev/Desktop/test.jpg")
> 
> else  
> @logos = Magick::ImageList.new("/home/rajeev/Desktop/test.jpg")  
> @logos = @logos.rotate(90)  
> @logos = @logos.write("/home/rajeev/Desktop/test.jpg")  
> end
> 
> I’m getting an image then creating a copy of it on the desktop. This is  
> then rotated and saved in the same location. So for the else statement i  
> want to say if the image is already on the desktop location then i just  
> want to rotate it again. Hope this makes sense, but the if…else block  
> doesn’t seem to work.
> 
> Would appreciate any help.
> 
> Cheers

Hi Rajeev

I hope this will help If you want to write if else statement in Rails it  
should be like this

If  
statement block  
elsif  
statement block  
else  
statement block  
end

Note : It is “elsif” not elseif

Hope this will help

Abhimanyu

---

<div class="post-metadata">

**Author:** ![ratschi85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/77aa72/32.png) [@ratschi85](https://www.ruby-forum.com/u/ratschi85)\
**Post date:** [October 25, 2007, 1:44pm UTC](https://www.ruby-forum.com/t/if-else-statement-problem/118297/3 "2007-10-25T13:44:42Z")

</div>

On first glance, it looks like your ‘else’ block won’t ever run unless  
there is some definition of @logos above that code that you had  
forgotten to paste. This may be why the second rotation doesn’t ever  
happen (if that is your problem).

Forgive me if I’m misinterpreting you.

- Edd  
[http://www.eddm.co.uk](http://www.eddm.co.uk)

On 10/25/07, Abhi M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > @logos = Magick::ImageList.new(“/home/rajeev/Desktop/test.jpg”)  
> > then rotated and saved in the same location. So for the else statement i
> 
> end
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Edd Morgan  
removed\_email\_address@domain.invalid  
+44 (0) 7979 474 043

---

<div class="post-metadata">

**Author:** ![ratschi85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/77aa72/32.png) [@ratschi85](https://www.ruby-forum.com/u/ratschi85)\
**Post date:** [October 25, 2007, 2:11pm UTC](https://www.ruby-forum.com/t/if-else-statement-problem/118297/4 "2007-10-25T14:11:36Z")

</div>

Hi –

On Thu, 25 Oct 2007, Rajeev K. wrote:

> That’s what I’m trying to do, but shouldn’t the else block execute if  
> the if statement is false?

Yes, and it will. I can promise you that if/else/end works 🙂

Where do you set @logos?

David

–  
Upcoming training by David A. Black/Ruby Power and Light, LLC:

- Advancing With Rails, Edison, NJ, November 6-9
- Advancing With Rails, Berlin, Germany, November 19-22
- Intro to Rails, London, UK, December 3-6 (by Skills Matter)  
See [http://www.rubypal.com](http://www.rubypal.com) for details!

---

<div class="post-metadata">

**Author:** ![ratschi85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/77aa72/32.png) [@ratschi85](https://www.ruby-forum.com/u/ratschi85)\
**Post date:** [October 25, 2007, 2:04pm UTC](https://www.ruby-forum.com/t/if-else-statement-problem/118297/5 "2007-10-25T14:04:21Z")

</div>

That’s what I’m trying to do, but shouldn’t the else block execute if  
the if statement is false?

Cheers

---

<div class="post-metadata">

**Author:** ![ratschi85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/77aa72/32.png) [@ratschi85](https://www.ruby-forum.com/u/ratschi85)\
**Post date:** [October 25, 2007, 3:07pm UTC](https://www.ruby-forum.com/t/if-else-statement-problem/118297/6 "2007-10-25T15:07:12Z")

</div>

That is correct, and syntactically I see nothing wrong with your code.  
However, from what I can see the if statement will always evaluate to  
true because @logos will always be null.

- Edd

On 10/25/07, Rajeev K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> That’s what I’m trying to do, but shouldn’t the else block execute if  
> the if statement is false?
> 
> ## Cheers
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Edd Morgan  
removed\_email\_address@domain.invalid  
+44 (0) 7979 474 043

---

<div class="post-metadata">

**Author:** ![ratschi85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/77aa72/32.png) [@ratschi85](https://www.ruby-forum.com/u/ratschi85)\
**Post date:** [October 25, 2007, 3:42pm UTC](https://www.ruby-forum.com/t/if-else-statement-problem/118297/7 "2007-10-25T15:42:26Z")

</div>

I’m still a bit confused with this even though it seems simple. If the  
if block gets executed the first time that means @logos has a value  
right? Which means the second time round the if statement will be false  
hence execute the else block. That’s what I understand anyways.

Basically what I want to say is if an image called test.jpg is already  
present on the desktop location (/home/rajeev/Desktop/test.jpg) then  
execute the else block (rotate the image again).

Sorry if I’m being to long I’m still new to this 🙂

Cheers

---

<div class="post-metadata">

**Author:** ![ratschi85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/77aa72/32.png) [@ratschi85](https://www.ruby-forum.com/u/ratschi85)\
**Post date:** [October 25, 2007, 5:35pm UTC](https://www.ruby-forum.com/t/if-else-statement-problem/118297/8 "2007-10-25T17:35:22Z")

</div>

On 25 Oct 2007, at 14:42, Rajeev K. wrote:

> I’m still a bit confused with this even though it seems simple. If the  
> if block gets executed the first time that means @logos has a value  
> right? Which means the second time round the if statement will be  
> false  
> hence execute the else block. That’s what I understand anyways.

Only if it’s the same object, since @logos is just an instance  
variable. If for example this was in a controller then the second  
time you hit the web page you’ll get a new instance of the controller  
and so @logos will be nil again.

Fred
