Live data from Hacker News

Optimizing Ruby Path Methods

byroot.github.io

41–50 of 66 posts

Re: Optimizing Ruby Path Methods

#41
post #25

Earlier quoted context omitted.

Because [] is an array with nothing in it, and [0] is an array with something in it. So saying “give me the array containing the first 100 elements of this array with one element” would obviously give you the array with one element back. Saying “give me the array containing the first 100 elements of this array with zero elements” would follow that it just gives the empty array back. On top of that, because ruby is hi…

Yeah, returning an empty array is pretty much exactly what I would expect given the first example. It would be a lot weirder to me if you were allowed to give an end index past the last element only if the array happened to be non-empty.

Sorry, I mis-spoke earlier, this is what I should have shared:

  [].slice(5, 100)
^-- *THIS* either returns nil or throws an exception.

Edit: Longer example:

  puts "[1, 2, 3].slice(1, 100) -> #{[1, 2, 3].slice(1, 100).to_s}"
  puts "[1, 2, 3].slice(3, 100) -> #{[1, 2, 3].slice(3, 100).to_s}"
  puts "[1, 2, 3].slice(4, 100) -> #{[1, 2, 3].slice(4, 100).to_s}"
Yields:

  [1, 2, 3].slice(1, 100) -> [2, 3]
  [1, 2, 3].slice(3, 100) -> []
  [1, 2, 3].slice(4, 100) -> 
So, there is a behavior difference between "array a little too short" and "array slightly more too short" that creates unexpected behavior.

That's not a big surprise in a tiny example like this; but if you expand this out into a larger code base, where you're just being an array and you want the 100 through 110th values for whatever reason - say it's a csv. Suddenly you're having to consider both the nil case and the empty array case; but then why are they different?

Re: Optimizing Ruby Path Methods

#42

Earlier quoted context omitted.

Frameworks and packages, sure. I’m not sure I would agree with APIs. ActiveAdmin is best in class, Rails is fantastic; but there’s a lot of insanity in the API for a language that “gets out of the way” and “just works” Slice is my favorite example. (It’s been a bit since I’ve used it) [0].slice(0, 100) == [0] [].slice(0, 100) == … exception? Or nil? Why does it equal []? For a “give me an array back that starts from…

because it's meant to be a more functional language. if slicing an array out of bounds threw an error it would be java. [].slice(0, 100).each do |x| puts x end that shouldn't be an error and it seems to be the principle of least surprise imo.

Sorry, I mis-spoke earlier, this is what I should have shared:

  [].slice(5, 100)
^-- *THIS* either returns nil or throws an exception.

( I made the other comment like this longer, please use that one for context )

Re: Optimizing Ruby Path Methods

#43

What happened to Ruby? It was very successful at some point. Maybe kids started using JS exclusively. But what happened to older developers? Did they move over? Rails seemed to enable very fast prototyping and iteration. Isn't it still the case? I see PHP usage going down, but PHP doesn't seem to have any advantages over JS, .NET, Python or Go. While Ruby coupled with Rails promised easy and rapid development. Of cou…

Ruby on Rails died because it was a resource Hog.

People moved to efficient IO requiring smaller servers.

If you make Ruby on Rails in a typed compiled language and show people how fast it is. People will switch in an eyeblink.

Re: Optimizing Ruby Path Methods

#45

What happened to Ruby? It was very successful at some point. Maybe kids started using JS exclusively. But what happened to older developers? Did they move over? Rails seemed to enable very fast prototyping and iteration. Isn't it still the case? I see PHP usage going down, but PHP doesn't seem to have any advantages over JS, .NET, Python or Go. While Ruby coupled with Rails promised easy and rapid development. Of cou…

Nothing has happened to Ruby, it's an excellent choice for software. We're basically at a point where majority of languages and stacks are on par with each other (unless you have niche requirements)

Re: Optimizing Ruby Path Methods

#46

What happened to Ruby? It was very successful at some point. Maybe kids started using JS exclusively. But what happened to older developers? Did they move over? Rails seemed to enable very fast prototyping and iteration. Isn't it still the case? I see PHP usage going down, but PHP doesn't seem to have any advantages over JS, .NET, Python or Go. While Ruby coupled with Rails promised easy and rapid development. Of cou…

PHP + Laravel give the same easy and rapid development as Ruby + Rails. Plus PHP reads as a programming language. Ruby reads as it was written by somebody with heavy brain damage struggling to put words into sentences.

Re: Optimizing Ruby Path Methods

#47
post #34

Makes me miss Ruby. Been in node typescript recently. Everything is a callback returning a promise in some weird resolution chain, mapped and conditional types, having to define schemas for everything and getting yelled at by lsp all day... Oh then you gotta write react components and worry about rerenders and undefined behavior caused by impurity in state, npm, arcane .json configs Versus active record, mvc, yaml co…

> Everything is a callback returning a promise in some weird resolution chain

Care to provide some examples of this? This hasn't been my experience, in general.

Re: Optimizing Ruby Path Methods

#48

> Given that the Intercom monolith CI runs with 1350 parallel workers by default Wow! I'd love to hear more about how that's achieved

It's because it probably uses Ruby on Rails which has to launch 1000s of os process to handle any traffic.

This makes no sense and has nothing to do with serving traffic.

More EC2 workers means more parallelism means tests map to more workers and the CI build finishes faster. It’s just CI perceived complete time.

Re: Optimizing Ruby Path Methods

#49
post #25

Earlier quoted context omitted.

Yeah, returning an empty array is pretty much exactly what I would expect given the first example. It would be a lot weirder to me if you were allowed to give an end index past the last element only if the array happened to be non-empty.

Sorry, I mis-spoke earlier, this is what I should have shared: [].slice(5, 100) ^-- *THIS* either returns nil or throws an exception. Edit: Longer example: puts "[1, 2, 3].slice(1, 100) -> #{[1, 2, 3].slice(1, 100).to_s}" puts "[1, 2, 3].slice(3, 100) -> #{[1, 2, 3].slice(3, 100).to_s}" puts "[1, 2, 3].slice(4, 100) -> #{[1, 2, 3].slice(4, 100).to_s}" Yields: [1, 2, 3].slice(1, 100) -> [2, 3] [1, 2, 3].slice(3, 100)…

Interesting! From playing around with it, seems like if the start index is exactly the same as the length, it returns empty array, but if it's further than that it returns nil. That's certainly not something I would have been able to predict, so I'd also be curious if anyone happens to know the explanation for it. My instinct is that it does seem like the type of edge case that might come up with a way to implement it tersely, but that's not a particularly good reason to leak that in the form of user-facing behavior, so hopefully there's a better explanation.

Some additional things I discovered when trying to figure out why it might work like that:

    * the behavior also seems consistent whether using `array.slice(a, b)` or `array[a..b]`
    * `array[array.length]` and `array[array.length + 1]` both return nil

Re: Optimizing Ruby Path Methods

#50
post #34

Makes me miss Ruby. Been in node typescript recently. Everything is a callback returning a promise in some weird resolution chain, mapped and conditional types, having to define schemas for everything and getting yelled at by lsp all day... Oh then you gotta write react components and worry about rerenders and undefined behavior caused by impurity in state, npm, arcane .json configs Versus active record, mvc, yaml co…

Lack of static types is one of the main reasons. Trying to decipher a complex ruby on rails codebase is unnecessarily difficult compared typescript. The tooling is also shit unless you use Ruby Mine. An absolute shame given how good the functionality is baked into RoR.

Skill issue
Post reply on HN