If else statement problem

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

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

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.

On 10/25/07, Abhi M. [email protected] 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/.


Edd Morgan
[email protected]
+44 (0) 7979 474 043

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 :slight_smile:

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 for details!

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

Cheers

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. [email protected] 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/.


Edd Morgan
[email protected]
+44 (0) 7979 474 043

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 :slight_smile:

Cheers

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs