Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Actually join is a method of Array https://ruby-doc.org/core-2.6.5/Array.html

A class that wants to support join has to implement to_s. The definition of join is

> join(separator=$,) → str

> Returns a string created by converting each element of the array to a string, separated by the given separator. If the separator is nil, it uses current $,. If both the separator and $, are nil, it uses an empty string.

If the class doesn't implement to_s Object.to_s kicks in and displays something like "#<SomeClass:0x000055a321fa2bb0>"

Enumerable is really a mixin https://ruby-doc.org/core-2.6.5/Enumerable.html but join is not listed in its methods.

There is another reply of mine with an example of a class can be used in join and doesn't mix in Enumerable.



oops, right you are, it's not defined on Enumerable (though i do wonder why! `each` does define an iteration order after all)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: