Given the foloowing code:
class Person
attr_accessor :first_name, :middle_name, :last_name
def whole_name
n = first_name + " "
n << "#{middle_name} " if middle_name
n << last_name
return n.size

m =
m.first_name = “Sam”
m.middle_name = “Ray”
m.last_name = “Smith”

x = m.whole_name

puts x

Why is x =13 instead of 3?

On Sat, Aug 30, 2008 at 3:02 PM, Ron G. [email protected] wrote:

Why is x =13 instead of 3?
You are asking about Array#push, yet not using it. If you would try to
replace ‘<<’ with ‘.push’ you would see that instead you are handling

irb(main):001:0> a = ‘Sam’
=> “Sam”
irb(main):002:0> a << ’ Ray’
=> “Sam Ray”
irb(main):003:0> a = [‘Sam’]
=> [“Sam”]
irb(main):004:0> a << ‘Ray’
=> [“Sam”, “Ray”]
irb(main):005:0> a = ‘Sam’
=> “Sam”
irb(main):006:0> a.push(‘Ray’)
NoMethodError: undefined method `push’ for “Sam”:String
from (irb):6
irb(main):007:0> a = [‘Sam’]
=> [“Sam”]
irb(main):008:0> a.push(‘Ray’)
=> [“Sam”, “Ray”]

I get it. Boy, was my stupid light lit or what?

Ron G. wrote:

Why is x =13 instead of 3?

Why not this?

def full_name
[@first_name, @middle_name, @last_name].compact.join(’ ')