CHarris
CHarris

Reputation: 2803

ruby on rails string issue

I've defined a method like so:

def users_followers
  if current_user == @user
    "People you're following"
  else
    "People "
    your_page(@user).chomp("Page")
    " following"
  end
end

I'm having trouble with the second part. All I am seeing is the word " following". Could you please help me with the best way to solve this?

In fact, it leaves out the space before 'following', which I need to keep.

Upvotes: 0

Views: 78

Answers (2)

qqx
qqx

Reputation: 19485

The method will return the results of the last statement. In the else block of that method that would be the statement " following". Since the results of the preceding lines of that section aren't assigned anywhere the results get ignored.

You can use string interpolation to create a single string to return:

"People #{your_page(@user).chomp("Page")} following"

Upvotes: 4

quandrum
quandrum

Reputation: 1646

That's because in your else, you have three statements, of which the last one is being returned. Concatenate them together:

def users_followers
  if current_user == @user
    "People you're following"
  else
    "People " +
    your_page(@user).chomp("Page") +
    " following"
   end
 end

Upvotes: 4

Related Questions