Ruby/String/Convert to String

Материал из Wiki.crossplatform.ru

Перейти к: навигация, поиск

call the to_s method on many built-in classes to return the contents of the object as a string

puts 1000.to_s
puts [1,2,3].to_s
puts ({ :name => "Fred", :age => 10 }).to_s



change the output on the fly with the %s format flag and %:

hi = "Hello, %s"
puts hi % "Tom!" # => "Hello, Tom!"
puts hi % "people!" # => "Hello, people!"
puts hi % "universe!" # => "Hello, universe!"



Convert an object to a string with to_s.

Ruby calls the to_s method from the class of the object, not the String class
parentheses are optional.
(256.0).class # => Float
(256.0).to_s # => "256.0"