Just learned about tagged logging in rails. Did you know you can tag a log by using
logger.tagged('Your Tag') { logger.info('Message') }
You can also do it globally with
# config/application.rb
config.log_tags = [:method_on_request_object, lambda { |request| request.method.modifier_method }]
Add client Device OS and OS Version information to logs (requires clients to send X-OS and X-OS-Version headers)
# config/application.rb
config.log_tags = [ :request_id, lambda { |request| "#{request.headers['HTTP_X_OS']} #{request.headers['HTTP_X_OS_VERSION']}" } ]
# app/models/user.rb
has_one :four_car_garage
has_many :cars
validates :cars, length: { maximum: 4 }
In terminal
vim todo.txt
Colon (:) (lowercase) q vim :q
Just don't use MacVim @joe :D
This is obviously a joke, it's impossible to quit Vim.
When you want to write destructive migrations please use if_exists: true when you’re trying to remove a table and check for a table existence when you want to add or remove a column ActiveRecord::Base.connection.table_exists?
Case 1:
def up
drop_table :kittens, if_exists: true
end
Case 2:
def up
return unless ActiveRecord::Base.connection.table_exists?(:kittens)
add_column :kittens, :kind, :integer
end
Open term (or equivalent) and paste the following one-liner:
defaults write com.apple.dock persistent-apps -array-add '{tile-data={}; tile-type="spacer-tile";}'; killall Dock
When the Dock restarts, there should be a draggable blank space that you can move anywhere. Repeat to your heart's desire.
$ curl -X POST -H 'Content-type: application/json' --data '{"text": "Something important"}' https://hooks.slack.com/services/YOUR/TOKENIZED/URL
{ "text": "Hello, Slack!", "username": "WebhookBot", "icon_emoji": ":ghost:" }
{ "text": "Hello, World!" }
curl -X POST -H 'Content-Type: application/json' --data @message.json https://hooks.slack.com/services/YOUR/TOKENIZED/URL
If you want to override previously set order (even through default_scope), use reorder() instead.
E.g.
User.order('id ASC').reorder('name DESC')
would ignore ordering by id completely
Ever wanted to display the structure of the current directory except or two directories?
#NPM or yarn project
tree -I node_modules
# Elixir Mix project
tree -I 'deps|test'
An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once
For example:
Create a new project using mix new anagram then let's write some test cases:
defmodule AnagramTest do
use ExUnit.Case
test "calculates anagram" do
assert Anagram.anagram?("restful", "fluster") == true
assert Anagram.anagram?("funeral", "realfun") == true
assert Anagram.anagram?("adultery", "truelady") == true
assert Anagram.anagram?("customers", "storescum") == true
assert Anagram.anagram?("fortyfive", "overfifty") == true
assert Anagram.anagram?("fiftyfive", "overfifty") == false
assert Anagram.anagram?("funeral", "real fun") == false
end
end
Now we have to write the code:
defmodule Anagram do
def anagram?(a, b) do
charlist(a) == charlist(b)
end
def charlist(w) do
w |> String.trim() |> String.downcase() |> String.graphemes() |> Enum.sort()
end
end
Run the tests mix test:
Compiling 1 file (.ex)
Generated anagram app
.
Finished in 0.1 seconds
1 test, 0 failures
Randomized with seed 752850
Done, now you have an anagram calculator!