[Quicky] Easy Ruby regexp to split string
![[Quicky] Easy Ruby regexp to split string](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1710205737600%2F94b9203f-e3b5-4dc2-a35f-6a8d7785fb77.png&w=3840&q=75)
10+ years most with Ruby on Rails and JavaScript frontend and some Go. My first language is C for 3 years in university.
Search for a command to run...
![[Quicky] Easy Ruby regexp to split string](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1710205737600%2F94b9203f-e3b5-4dc2-a35f-6a8d7785fb77.png&w=3840&q=75)
10+ years most with Ruby on Rails and JavaScript frontend and some Go. My first language is C for 3 years in university.
No comments yet. Be the first to comment.
I recently updated from Slim template to ERB to use Herb (Thanks Marco). I had to update a test that was very strict on the format. I realized that Capybara::Node::Simple doesn’t provide a selector for regex. The page object doesn’t have a match? met...

It is as simple as: rustup completions zsh > ~/.zfunc/_rustup rustup completions zsh cargo > ~/.zfunc/_cargo With a TAB you get the auto-complete or the options available: ❯ cargo b b -- alias: build bench -- Execute all benchmarks of a local ...
Let’s not re-write a good article, go to: https://joyofrails.com/articles/mastering-custom-configuration-in-rails I mostly used config.x configuration that loads a yaml file with config_for that do the multi-environment by default: module YourApp c...
No need to re-write the wheel, let me redirect to this great article from Lily Reile. Obviously still up-to-date and really useful as the official Rails doc is mostly showing the value before and after. https://lilyreile.medium.com/rails-6-1-new-fram...
I came around to update Appsignal gem to the new major version of Appsignal released at the end of August 2024: https://rubygems.org/gems/appsignal/versions/4.0.0 The upgrade was smooth. The real change for my application was to remove the parameter ...
Are you good at regexp? I'm not and I found an extremely easy way to update a regexp to split a string in Ruby.
abc,abc
abc abc
abc
I just want to split on , , a space ... and a new line. Great
I won't show you an horrible .split with a .map then redo a .split again.
regexp = /[ ,\n]/
input.split(regexp)
It is annoying to read (even that simple) and it did not work for what I wanted. Remember the "new line", as a html input/textarea is not an \n but an \r\n .
You can change the regexp to /[ ,\r\n]/ but it split twice, once for \r then once more for \n. And the readability is lower.
regexp = Regexp.union([" ", "\r\n", "\n", ","])
input.split(regexp)
You give the string you want the regexp to split on and it generates the proper Regexp for you.
It won't solve all but for me, it solve most of my simple cases with code that I use everyday: a dead simple list of string.
Share it!
Cheers, Thomas