Huy Tran
Huy Tran

Reputation: 1902

Implement to_s of Array in Ruby

Ruby's Array class has the built-in method to_s that can turn the array into a string. This method also works with multidimensional array. How is this method implemented?

I want to know about it, so I can reimplement a method my_to_s(ary) that can take in a multidimensional and turn it to a string. But instead of returning a string representation of the object like this

[[[1,2,3, Person.new('Mary')]],[4,5,6,7], Person.new('Paul'),2,3,8].to_s
# [[[1, 2, 3, #<Person:0x283fec0 @name='Mary']], [4, 5, 6, 7], #<Person:0x283fe30 @name='Paul'>, 2, 3, 8]   

my_to_s(ary) should call the to_s method on these objects, so that it returns

my_to_s([[[1,2,3, Person.new('Mary')]],[4,5,6,7], Person.new('Paul'),2,3,8])
# [[[1, 2, 3, Student Mary]], [4, 5, 6, 7], Student Paul>, 2, 3, 8]

Upvotes: 0

Views: 516

Answers (1)

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121000

For nested elements it just calls to_s respectively:

def my_to_s
  case self
  when Enumerable then '[' << map(&:my_to_s).join(', ') << ']'
  else 
    to_s # or my own implementation
  end
end

This is a contrived example, that nearly works, if this my_to_s method is defined on the very BasicObject.


As suggested by Stefan, one might avoid monkeypathing:

def my_to_s(object)
  case object
  when Enumerable then '[' << object.map { |e| my_to_s(e) }.join(', ') << ']'
  else 
    object.to_s # or my own implementation
  end
end

More OO approach:

class Object
  def my_to_s; to_s; end
end

class Enumerable
  def my_to_s
    '[' << map(&:my_to_s).join(', ') << ']'
  end
end

class Person
  def my_to_s
    "Student #{name}"
  end
end

Upvotes: 2

Related Questions