3 ms·
I understand why Python does it this way, but this is usually where someone points out Ruby doing: somestring=somearray.join(' ') Versus Python's: somest
by tyingq 5y ago
I understand why Python does it this way, but this is usually where someone points out Ruby doing:
somestring=somearray.join(' ')
Versus Python's:
somestring=' '.join(somearray)
- dragonwriter 5y agoI generally like Ruby better than Python, but Python's choice on _join_ being a string method on the thing stuck between elements rather than an collection method on the things whose elements are getting something stuck between is better.
- renox 5y agoWhy do you think it's better? I don't use either Ruby or Python but I think that the first way is more natural.
- renewiltord 5y agoInteresting. I model `array.join(',')` or the equivalent Py `','.join(l)` in my head as ultimately (pseudo-code): foldLeft(tail(array), str(head(elem)), (acc: String, elem: T) -> acc + ',' + str(elem)) Which makes the array method easier to understand than the string method. Funny how I thought it was _obvious_, but you find the opposite obvious.
- mcphage 5y agoWhy? somearray is the important piece, the string is just a bit of spacing. Put the important thing first, don’t bury the lede.
- tedunangst 5y agoprint('Hello '.join('world!'))
- pmontra 5y agoAfter years of Python I can't remember which one of these are correct 1 ",".join(["a", "b"]) 2 ["a", "b"].join(",") 3 ",".split("a,b") 4 "a,b".split(",") These method names feel like active form so 1 and 3 should be correct but it's 1 and 4 despite "a,b" obviously is not splitting "," but it's splitted by it In Ruby it's 2 and 4. I'm out of luck twice but at least Ruby has a and b always to the left and the comma always to the right so I always remember it.