Skip to content

Instantly share code, notes, and snippets.

View garyharan's full-sized avatar

Gary Haran garyharan

View GitHub Profile
@garyharan
garyharan / maximum_processes.ex
Created September 27, 2018 17:51
How to calculate maximum number of processes in Elixir
0..1_000_000
|> Enum.each(fn(_n) ->
IO.puts "Creating process: #{Process.list |> Enum.count}"
spawn(fn -> Process.sleep(:infinity)
end)
end)
@garyharan
garyharan / uncommit_and_stash.sh
Created February 7, 2019 16:02
How to stash your unpushed commits
$ git status
5 commits pending
$ git reset --soft HEAD~5 # where 5 is the number of commits you saw in the `git status`
$ git stash
@garyharan
garyharan / split_pushed_commits.sh
Created February 24, 2020 17:52
Workflow for splitting commits already pushed
# initiate the change with an interactive rebase from the point you want
git rebase -i SHA^
git reset HEAD^
# do this as many times as you need.
git add -p
git commit -v
# once all your changes are committed
git rebase --continue
@garyharan
garyharan / fix_mac_audio.sh
Created March 17, 2020 20:02
Restart all core audio when airpods do not work
# Instead of rebooting system
# If audio has half going through speakers and half going through headphones.
ps aux | grep 'coreaudio[d]' | awk '{print $2}' | xargs sudo kill
@garyharan
garyharan / experience.txt
Last active June 7, 2021 14:05
Experience and diversity have value
A friend of mine, approaching 50, works as a programmer in a medium sized consultancy. In a recent conversation he told me how he felt like he was feeling older now that they had hired many 20-somethings in his company. The younger developers, he said, would do things faster than he would and it started to look like he was just a slower developer than the more youthful new hires. He was wondering if his days were numbered.
He was resigning himself to the idea that he was experiencing some kind of decline of his faculties if he compared himself to the more youthful years of his career. However this wasn’t the only conversation I had about the work at his company. The conversation before that one had him put the finger on something crucial.
So I had a conversation with him about it and we connected the dots.
Me: What was the password story they tackled?
Him: A password reset.
Me: And what was wrong with how they implemented it.
Him: Well the user typed their email in and then if they click the “forgot passw
@garyharan
garyharan / story_time_classy_switch_cases.rb
Last active May 12, 2022 16:05
Using a class comparison in a switch case condition
# Story time: Classy Switch Classes
# Say you have a bunch of messages coming in Slack and you'd like to add an automatic emoji
# of course your team is multicultural and you may want to cater to the diverse crowd you have.
# For the purpose of this exercise we will treat `message#emojis` and `message#replies` as arrays we can push to.
# You could write your code like this:
if message.text.include?("Good morning!")
@garyharan
garyharan / height_difference.md
Created December 19, 2022 17:01
ActiveRecord order by SQL generated value

If you want to create an extra attribute based on SQL query with ActiveRecord you can use the following:

@games = Game.select("*, ABS(#{@profile.level} - level) AS level_difference")

Then in your Ruby code you can refer to things this way:

@games.first.level_difference # => 16
@garyharan
garyharan / test_helper.rb
Created December 27, 2022 16:16
How to test using capybara in latest rails and chrome
# as per https://rubydoc.info/github/teamcapybara/capybara/master#using-capybara-with-minitest
require 'capybara/rails'
require 'capybara/minitest'
class ActionDispatch::IntegrationTest
# Make the Capybara DSL available in all integration tests
include Capybara::DSL
# Make `assert_*` methods behave like Minitest assertions
include Capybara::Minitest::Assertions
@garyharan
garyharan / installation.md
Created February 1, 2023 14:49
How to install turbo-ios
@garyharan
garyharan / back_to_the_tab.md
Last active March 1, 2023 18:02
RFC How to reopen existing tab

We need a way back to the tab when resetting passwords or getting a token from email or second factor authentication.

Too often we open a bunch of tabs and forget which one was open.

Imagine if we could create a token that would reopen the given tab that initiated the exit.

window.security.tabIdToken // returns: 35c7b72b-00bd-4734-81f7-318392f52370