-
-
Save pmodin/d46ea10e53385daa1442 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# Ruby Project Night 2014-09-22 @ SUP46, Stockholm | |
# Why does the to_s methods yeild different results? | |
# What am I doing wrong and how should I do different? | |
# | |
# After a good nights sleep I still cannot wrap my head around this. | |
# | |
# ruby -v : ruby 2.0.0p353 (2013-11-22 revision 43784) [x86_64-linux] | |
# Internal list class | |
class List | |
attr_reader :list | |
def initialize(elem) | |
@list = Array.new(elem) | |
end | |
# method 1 | |
def to_s | |
@list.to_s | |
end | |
# method 2 | |
# def to_s | |
# @list | |
# end | |
end | |
list = List.new(%w(1 2 3)) | |
puts list # method 1: ["1", "2", "3"] | |
# method 2: #<List:0x00000002587648> | |
puts list.list # 1\n2\n3\n <= these are actual newlines in console |
https://github.com/ruby/ruby/blob/d198d64e0464c141f70c49880bf511ac3dcd1162/io.c#L7097-L7126
If it is enumerable, and the value is a string, then it prints it with a newline.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Shorter example: