This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
irb(main):001:0> h = { a: 1, b: 2 } | |
=> {:a=>1, :b=>2} | |
# each_with_object same as reduce or inject, memo is a new hash. | |
# unpack arguments in block | |
irb(main):002:0> h.each_with_object({}) {|(k,v),new_hash| new_hash[v] =k } | |
=> {1=>:a, 2=>:b} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a =[{n: 1}, {n: 5}, {n: 3}] | |
a.sort {|x,y| x[:n]<=>y[:n] } #=> [{:n=>1}, {:n=>3}, {:n=>5}] | |
a.sort_by{|x| x[:n].to_i } #=> [{:n=>1}, {:n=>3}, {:n=>5}] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
→ git remote prune origin | |
→ git branch --merged | grep -v "\*" | xargs -n 1 git branch -d | |
# Remember to do this from the master branch | |
# If you run it from a feature branch the master branch gets deleted. | |
→ git branch --merged | grep -v "\*" | xargs -n 1 git branch -d | |
Deleted branch master (was 644eb76). | |
You can recheckout master branch | |
→ git checkout -b master origin/master |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
irb(main):040:0> testcases | |
=> ["hello world", "'hello world", "'hello' world", "hello' world"] | |
irb(main):041:0> testcases.each { |i| puts /(?<!')hello(?!')/.match(i) } | |
hello | |
=> ["hello world", "'hello world", "'hello' world", "hello' world"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
audience_range_values = [] | |
age_from = nil | |
audience_range_values << age_from || 3 # does not work, returns book.a | |
print audience_range_values | |
audience_range_values = [] | |
# output [nil] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Many concepts here to revisit, so saving in gist. | |
http://stackoverflow.com/questions/21712814/how-can-i-extract-the-bigger-value-for-each-key-in-a-list-of-hashes-in-ruby/21729254?noredirect=1#comment32860522_21729254 | |
a = [{1=>19.4}, {1=>12.4}, {2=>29.4}, {3=>12.4}, {2=>39.4}, {2=>59.4}] | |
# the below is the main trick, to group the hashes and sorted the key/value pair | |
# in **ascending order**. | |
a.sort_by(&:to_a) | |
# => [{1=>12.4}, {1=>19.4}, {2=>29.4}, {2=>39.4}, {2=>59.4}, {3=>12.4}] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
irb(main):001:0> 'A' * 3 | |
=> "AAA" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# I thought this was neat | |
# Reference: http://stackoverflow.com/questions/21404323/combining-two-different-ranges-to-one-in-ruby | |
[*'0'..'9', *'a'..'z', *'A'..'Z'].shuffle.first(2).join | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function genericallyCallingMap(dna) { | |
var complements = {"C":"G", "G":"C", "T":"A", "A":"U"}; | |
var map = Array.prototype.map | |
var a = map.call(dna, function(x) { | |
return complements[x]; | |
}) | |
return a.join(""); | |
}; | |
function callingMapOnAnArray(dna) { | |
var a = dna.split("").map( function(x) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Bst | |
attr_accessor :data, :left, :right | |
def initialize(data) | |
self.data = data | |
end | |
def insert_r(data) | |
if (self.right ) |