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
So perhaps so could correct/extend the dokumentation...
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)?'[]':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
So perhaps so could correct/extend the dokumentation...
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)?'[]':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
> 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?
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]"
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:
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.
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.