Sara
Sara

Reputation: 2769

Output value of puts in Ruby

I'm probably missing something really obvious here. I have the following Ruby method:

def pair_array

  return self.pair.each_slice(2) {
    |x| puts x.join(" & ")
  }.to_s
end

When I try to display the value of this method in my Rails view by calling @team.pair_array nothing appears, but the correct value gets printed on the console. I know this is probably because I'm using puts. How can I get the result of this method to display in my view?

Upvotes: 0

Views: 294

Answers (1)

Chuck
Chuck

Reputation: 237110

You're confusing printing with returning a value. puts returns nil, and each_slice does not return the result of the block anyway. What you want is this:

def pair_array
  pair.each_slice(2).map {|arr| arr.join ' & '}
end

Upvotes: 5

Related Questions