Leahcim
Leahcim

Reputation: 42049

If statement in Ruby giving me unexpected answer

I'm playing around with the Ruby Twitter gem, which, for example, allows you to check if a Twitter user's account is locked. The user's account I am testing is locked.

If I do

@awish = Twitter.user("twitteruser").protected?

and then in a view do

 <%= @awish %> 

it says "true"

However. when I try to write a method to test this and assign the response to a variable depending on whether it's locked or not, it's telling me that it's "not locked", when I would be expecting it to say it's "locked". So I think I've screwed up this if/else statement somehow (note, I'm a bit of a noob)

Can you explain what I've done wrong?

def awisprotect
       @awish = Twitter.user("twitteruser").protected?
       if @awish == "true" 
        @answer = "locked" 
        else 
        @answer = "not locked"
        end

       render :js => "$('div.awishanswer').html(' #{ @answer } ');"
   end

Upvotes: 0

Views: 42

Answers (2)

Matthew Rudy
Matthew Rudy

Reputation: 16844

the protected? method should either return true or false.

(rather than "true" or "false")

So your method can be much simpler. (you also don't need an instance variable)

def awisprotect
  if Twitter.user("twitteruser").protected?
    answer = "locked" 
  else 
    answer = "not locked"
  end
  render :js => "$('div.awishanswer').html(' #{ answer } ');"
end

Upvotes: 2

dj2
dj2

Reputation: 9598

The "true" isn't a string, remove the quotes and it should be fine.

Upvotes: 2

Related Questions