TIL is my collection of short, technical daily learnings. 198 and counting.
Truthy Strings
Ruby strings are truthy, and they evaluate to zero, unless they start with a number. > 'nine to five'.to_i => 0 > '9 to 5'.to_i => 9
TIL is my collection of short, technical daily learnings. 198 and counting.
Ruby strings are truthy, and they evaluate to zero, unless they start with a number. > 'nine to five'.to_i => 0 > '9 to 5'.to_i => 9
Vim can find and replace strings across files, just like other text editors. It’s really (sort of) easy. First load all the files you want to change into the buffer with a splatted directory. :args path/to/files/*/* Then, execute the substitution. :argdo %s/old_string/new_string/ge | update The e flag is important; it tells Vim not to issue an error if the search pattern fails. This will prevent No Match errors from breaking the chain.
Ruby’s Enumerable class includes minmax, which returns a two element array with the minimum and maximum values of an enumerable. > [1, 5, 10].minmax => [1, 10] > ['alpha', 'bravo', 'zulu'].minmax => ["alpha", "zulu"]
To call a Vimscript method in Vim, first source the file. :source ~/path/to/vimscript.vim Next, put the cursor over the method call, and run that line. :exec getline(".")
You can make Ruby regex matchers case insensitive with an i. 2.1.0 :001 > 'Expected output' =~ /expected/ => nil 2.1.0 :002 > 'Expected output' =~ /expected/i => 0 This is useful when writing tests, when you care about a message, but are not interested in its exact format.
Check the value of any Vim setting by adding a ? to the end of its name. # Validate spelling is turned off :set spell? => nospell # Validate incremental search is turned on :set incsearch? => :incsearch
With Vim you can jump down any n amount of lines with n + j, and back up with n + k. An alternate command is n + +, which jumps down to the first non-blank character, and n + -, which jumps back up to the first non-blank character.
To redirect a path in Rails: # config/routes.rb get "/authors" => redirect("/designers") This lets you take a difficult-to-type path name and expose it to your users as a simpler path name: # config/routes.rb get "/login" => redirect("/auth/google_oauth2")
Rails comes with HTTP Basic authentication built in. Adding this to a controller is how it’s done: # app/controllers/application_controller.rb http_basic_authenticate_with name: 'name', password: 'password' When you visit a URL requiring authentication, you can bypass the authentication screen by adding your username and password to the beginning of the URL. For localhost, this would look like: name:password@localhost:3000
To save a snapshot of your current work in git, try this command: git stash save "snapshot: $(date)" && git stash apply "stash@{0}" This saves your current work in a timestamped stash, without removing it. In Hashrocket’s dotmatrix this command is aliased to git snapshot.
Don’t miss my next essay
Hear from me immediately when I post: no ads, unsubscribe anytime.