3 ms·
Principle of least surprise: make it work like >>> f"{1.5} {'hello'} {None}" '1.5 hello None' Ruby does that. From [1] "[Array#join] Returns a string crea
by pmontra 6y ago
Principle of least surprise: make it work like
>>> f"{1.5} {'hello'} {None}"
'1.5 hello None'
Ruby does that. From [1] "[Array#join] Returns a string created by converting each element of the array to a string, separated by the given separator."
The difference is that nil (Ruby's None) disappears
2.6.5 :001 > [1.5, "hello", nil].join(" ")
=> "1.5 hello "
That's totally expected because in Ruby the conversion of nil into a string is the empty string. Python converts it into the string "None".
[1] https://ruby-doc.org/core-2.6.5/Array.html#method-i-join https://ruby-doc.org/core-2.6.5/Array.html#method-i-join
Edit: an example with a type that normally wouldn't meaningfully cast to string
2.6.5 :001 > class Example
2.6.5 :002?> attr_accessor :name
2.6.5 :003?> end
=> nil
2.6.5 :004 > e = Example.new
=> #<Example:0x000055a321fa2bb0>
2.6.5 :005 > e.name = "example"
=> "example"
2.6.5 :006 > e.to_s
=> "#<Example:0x000055a321fa2bb0>"
2.6.5 :007 > [1, e].join(" ")
=> "1 #<Example:0x000055a321fa2bb0>"
2.6.5 :008 > class Example
2.6.5 :009?> attr_accessor :name
2.6.5 :010?> def to_s
2.6.5 :011?> "#{name}"
2.6.5 :012?> end
2.6.5 :013?> end
=> :to_s
2.6.5 :014 > e = Example.new
=> #<Example:0x000055a321f8a4c0>
2.6.5 :015 > e.name = "example"
=> "example"
2.6.5 :016 > e.to_s
=> "example"
2.6.5 :017 > [1, e].join(" ")
=> "1 example"