[].join

Hi

p [0,[1,2,[3,4,5],6],7].join => 01234567
Doku says: joining by converting each element to a string. (Thats all)
This array contains 3 elements, so it would be "0 [elem1] 7"
because arr.to_s = "[arr]"

Is there an "outer-only" join,
which does what the documentation tells, or how can I archive that (with variable seperator)?
(I think it would not be a good idea to change the behaviour of join :wink:
So perhaps so could correct/extend the dokumentation...

thanks Opti

Hi

p [0,[1,2,[3,4,5],6],7].join => 01234567
Doku says: joining by converting each element to a string. (Thats all)
This array contains 3 elements, so it would be "0 [elem1] 7"
because arr.to_s = "[arr]"

Are you sure about that last point?

Is there an "outer-only" join,
which does what the documentation tells, or how can I archive that (with

variable seperator)?

You could use #map to convert any inner arrays to whatever you want, then
#join the result... I guess.

···

On 14 Jan 2017 22:00, "Die Optimisten" <inform@die-optimisten.net> wrote:

~~~
arr.map{|x|x.is_a?(Array)?'[:skull_and_crossbones:]':x}.join
~~~

I'm still not sure exactly what the problem is, though, or what you'd
prefer to have happen.

(I think it would not be a good idea to change the behaviour of join :wink:
So perhaps so could correct/extend the dokumentation...

thanks Opti

Hi, I think the problem is in the docs (
Class: Array (Ruby 2.4.0)):

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 empty string.

It does not say that #join is recursive method (it is applies to all
subarrays of an array) and only way to find this out, one should look into
ruby source code.

···

On Sat, Jan 14, 2017 at 3:42 PM, Matthew Kerwin <matthew@kerwin.net.au> wrote:

On 14 Jan 2017 22:00, "Die Optimisten" <inform@die-optimisten.net> wrote:
>
> Hi
>
> p [0,[1,2,[3,4,5],6],7].join => 01234567
> Doku says: joining by converting each element to a string. (Thats all)
> This array contains 3 elements, so it would be "0 [elem1] 7"
> because arr.to_s = "[arr]"
>

Are you sure about that last point?

>
> Is there an "outer-only" join,
> which does what the documentation tells, or how can I archive that (with
variable seperator)?
>

You could use #map to convert any inner arrays to whatever you want, then
#join the result... I guess.

~~~
arr.map{|x|x.is_a?(Array)?'[:skull_and_crossbones:]':x}.join
~~~

I'm still not sure exactly what the problem is, though, or what you'd
prefer to have happen.

>
> (I think it would not be a good idea to change the behaviour of join :wink:
> So perhaps so could correct/extend the dokumentation...
>
> thanks Opti

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

--
С наилучшими пожеланиями,
Артем Байкузин.

>
> Hi
>
> p [0,[1,2,[3,4,5],6],7].join => 01234567
> Doku says: joining by converting each element to a string. (Thats all)
> This array contains 3 elements, so it would be "0 [elem1] 7"
> because arr.to_s = "[arr]"
>

Are you sure about that last point?

Hi Matthew
ok: arr.to_s = "arr" # and arr is shown as [...]
Artem mentioned it: outerjoin should join, but not recursively.
And "Recursively" should be added to the documentation of join (=> therefore could better be named rjoin)

> I'm still not sure exactly what the problem is, though, or what you'd prefer to have happen.

'[skull]' (just was too lazy to copy the whole text) --- I meant:
[0,[1,2,[3,4,5],6],7].myjoin(' + ') # => "[0 + [1,2,[3,4,5],6] + 7]"
yes thanks for the solution with map (it's a little complicated for a just normal non-recursive join, but it works)

Opti

···

On 2017-01-14 13:42, Matthew Kerwin wrote:

On 14 Jan 2017 22:00, "Die Optimisten" <inform@die-optimisten.net > <mailto:inform@die-optimisten.net>> wrote:

I agree that the docs are not clear about this behavior;
it should be fixed soon.

Regards,
Marcus

···

Am 14.01.2017 um 23:13 schrieb Die Optimisten:

> Doku says: joining by converting each element to a string. (Thats all)
> This array contains 3 elements, so it would be "0 [elem1] 7"
> because arr.to_s = "[arr]"

--
GitHub: https://github.com/stomar/
PGP: 0x6B3A101A

​Why do you keep writing things like this? I still don't understand. Do you
mean:

···

On 15 January 2017 at 08:13, Die Optimisten <inform@die-optimisten.net> wrote:

On 2017-01-14 13:42, Matthew Kerwin wrote:
​​





Hi Matthew
ok: arr.to_s = "arr" # and arr is shown as [...]

~~~
[1,2,3].to_s = "[1,2,3]"
~~~

? If so, please write it. I'm not at my Ruby development environment at the
moment, and I can't remember which Array#to_s is currently in play, because
in previous Ruby versions it's gone through iterations where it looked like
the output of #join, or #inspect, or somewhere in between.​

Artem mentioned it: outerjoin should join, but not recursively.
And "Recursively" should be added to the documentation of join (=>
therefore could better be named rjoin)

> I'm still not sure exactly what the problem is, though, or what you'd
prefer to have happen.

'[skull]' (just was too lazy to copy the whole text) --- I meant:
[0,[1,2,[3,4,5],6],7].myjoin(' + ') # => "[0 + [1,2,[3,4,5],6] + 7]"
yes thanks for the solution with map (it's a little complicated for a just
normal non-recursive join, but it works)

​Not that complicated, really, if #to_s does what you want:

~~~
arr.map(&:to_s).join
~~~

Cheers
--
  Matthew Kerwin
  http://matthew.kerwin.net.au/

​If anyone's interested in the history, this behaviour is almost as old as
my children, and goes back to the dawn of 1.9:

https://bugs.ruby-lang.org/projects/ruby-trunk/repository/revisions/23951
https://bugs.ruby-lang.org/projects/ruby-trunk/repository/revisions/26906

Also check the changes to things like Array#to_s from 1.8 to 1.9

  "Returns self.join."

  "Create a printable version of array."

The good old days

···

On 15 January 2017 at 09:06, <sto.mar@web.de> wrote:

Am 14.01.2017 um 23:13 schrieb Die Optimisten:
>> > Doku says: joining by converting each element to a string. (Thats all)
>> > This array contains 3 elements, so it would be "0 [elem1] 7"
>> > because arr.to_s = "[arr]"

I agree that the docs are not clear about this behavior;
it should be fixed soon.

Regards,
Marcus

--
  Matthew Kerwin
  http://matthew.kerwin.net.au/

To be precise, the current behavior (2.4.0) is:

[1,2, 3 ].to_s # => "[1, 2, 3]"

Note the whitespace.

Opti, generally it's easier to help when you provide actual, working
code snippets together with the result, e.g. copied from an irb session.
That's how it's done in the docs (usually) and in many blog posts and
tutorials, and makes it much easier for the group to test the code
(by pasting it into irb or pry) and providing a solution.

And using more descriptive subject lines, e.g. something like
"Array#join for nested arrays" would also be helpful.

Regards,
Marcus

···

Am 15.01.2017 um 00:40 schrieb Matthew Kerwin:

    Hi Matthew
    ok: arr.to_s = "arr" # and arr is shown as [...]

​Why do you keep writing things like this? I still don't understand. Do
you mean:

~~~
[1,2,3].to_s = "[1,2,3]"
~~~

? If so, please write it. I'm not at my Ruby development environment at
the moment, and I can't remember which Array#to_s is currently in play,
because in previous Ruby versions it's gone through iterations where it
looked like the output of #join, or #inspect, or somewhere in between.​

--
GitHub: stomar (Marcus Stollsteimer) · GitHub
PGP: 0x6B3A101A

Fixed (hopefully), see

https://docs.ruby-lang.org/en/trunk/Array.html#method-i-join

Regards,
Marcus

···

Am 15.01.2017 um 00:06 schrieb sto.mar@web.de:

I agree that the docs are not clear about this behavior;
it should be fixed soon.

--
GitHub: stomar (Marcus Stollsteimer) · GitHub
PGP: 0x6B3A101A

Cool!!! :wink::clap::+1:

https://bugs.ruby-lang.org/projects/ruby-trunk/repository/revisions/57329

Abinoam Jr.

···

Fixed (hopefully), see

class Array - Documentation for Ruby 3.4

Regards,
Marcus

Hi Marcus!
Very nice example!
Can everyone change that (trunk), or are you a Ruby core-developer?

thanks
Opti

···

On 2017-01-15 10:41, sto.mar@web.de wrote:

Am 15.01.2017 um 00:06 schrieb sto.mar@web.de:

I agree that the docs are not clear about this behavior;
it should be fixed soon.

Fixed (hopefully), see

class Array - Documentation for Ruby 3.4

Regards,
Marcus

I'm not on ruby-core, and luckily not everyone can change trunk :slight_smile:

I submitted a patch to bugs.ruby-lang.org (which has been applied
pretty fast, usually it takes just a bit longer).

Regards,
Marcus

···

Am 15.01.2017 um 18:33 schrieb Die Optimisten:

Can everyone change that (trunk), or are you a Ruby core-developer?

--
GitHub: stomar (Marcus Stollsteimer) · GitHub
PGP: 0x6B3A101A