Skip to content

Instantly share code, notes, and snippets.

View hyfather's full-sized avatar

Nikhil Mungel hyfather

View GitHub Profile
@hyfather
hyfather / ssh-copy-id
Created June 9, 2011 11:11 — forked from achamian/ssh-copy-id
Copy public key to authorized_keys
#!/usr/bin/env sh
# Copy public key to authorized_keys of a remote machine
if [ -z $1 ];then
echo "Usage"
echo "ssh-copy-id hostname /path/to/public/key"
exit
fi
if [ -z $2 ]; then
KEYCODE=`ssh-add -L`
@hyfather
hyfather / minusminusversion.sh
Created September 12, 2011 16:22
I redirect this snippet's output into a version file that forms for good metadata about a deployable package which usually doesn't have the git repository along with it.
git log HEAD^..HEAD --format="%an pushed change %h on %aD.%n%nWhat's in this change?%n%s%n"
@hyfather
hyfather / flatten_hashes.rb
Created October 18, 2011 11:53
[Ruby] Merge all the hashes inside an Array into one big hash
class Array
def flatten_hashes
Hash[*self.map(&:to_a).flatten]
end
end
[{:a => 'alpha'}, {:b => 'beta'}, {:c => 'charlie'}].flatten_hashes
#=> {:a => 'alpha', :b => 'beta', :c => 'charlie'}
@hyfather
hyfather / hashes_and_ararys.rb
Created October 24, 2011 12:41
Demonstrates how to form Hashes from Arrays in Ruby.
{:a => 'alpha'}.to_a
#=> [[:a, "alpha"]] # Notice the nested array
Hash[:a => 'alpha']
#=> {:a => 'alpha'}
Hash[[:a => 'alpha']]
#=> {} # Doesn't work with a nested array.
Hash[[[:a => 'alpha']]]
@hyfather
hyfather / array_map.rb
Created October 24, 2011 13:06
Demonstrates how :map can be used on class:Array
[:a, :b, :c].map {|e| e.to_s }
#=> ['a', 'b', 'c'] # Invoke .to_s on each element and collect these in an array.
[:a, :b, :c].map(&:to_s)
#=> ['a', 'b', 'c'] # The same result obtained in a shorthand fashion.
@hyfather
hyfather / splat.rb
Created October 24, 2011 13:49
Demonstrates how the splat operator works in Ruby.
lambda{|*args| args}.call(1, 2)
#=> [1, 2]
# This is the splat operator used in the 'collecting mode'.
# The splat before the args ensured that all arguments were marshalled into an Array.
[1, 2, [3, 4]]
#=> [1, 2, [3, 4]]
[1, 2, *[3, 4]]
#=> [1, 2, 3, 4]
# This is the unmarshalling 'mode' of the splat operator.
@hyfather
hyfather / merging_hashes_explained.rb
Created October 24, 2011 14:35
Explains how all Hashes in an Array can be merged.
[{:a => 'alpha'}, {:b => 'beta'}, {:c => 'charlie'}].map(&:to_a)
#=> [[[:a, "alpha"]], [[:b, "beta"]], [[:c, "charlie"]]]
flat_array = [{:a => 'alpha'}, {:b => 'beta'}, {:c => 'charlie'}].map(&:to_a).flatten
#=> [:a, "alpha", :b, "beta", :c, "charlie"]
Hash[*flat_array]
#=> {:a=>"alpha", :b=>"beta", :c=>"charlie"}
@hyfather
hyfather / emacs_readline_keybindings.md
Created October 25, 2011 04:40
Shortcuts with the emacs readline

M stands for the Meta key. Commonly represented by the Alt/Option/Esc key on your keyboard.
C stands for the Control key.
Example -- C-k means 'hit k while keeping Control pressed.'

M-.
Yank the last argument to the previous command.

M-C-y
Yank the first argument to the previous command.

@hyfather
hyfather / euler_eight.rb
Created November 9, 2011 04:49
My solution to problem #8 on projecteuler.net
#"Find the greatest product of five consecutive digits in the 1000-digit number."
# http://projecteuler.net/problem=8
big = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522\
74430435576689664895044524452316173185640309871112172238311362229893423380308135336276614282806444486645238749303589072962904915604407723907138105158593079608667017242712188399879790879227492190169\
97208880937766572733300105336788122023542180975125454059475224352584907711670556013604839586446706324415722155397536978179778461740649551492908625693219784686224828397224137565705605749026140797296\
86524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054\
4217506941658960408071984038509624554443629812309878799272442849091888458015616609791913387549920052406368991
@hyfather
hyfather / euler_one.rb
Created November 13, 2011 20:34
My solution to problem #1 on projecteuler.net
p [1..999].select{|e| e%3 == 0 || e%5 == 0}.inject(:+)