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
#!/usr/bin/env ruby | |
threads = 100.times.map do |n| | |
Thread.new n do |m| | |
base = 1 + (m * 100) | |
100.times.map do |i| | |
base + i | |
end | |
end | |
end |
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
#!/usr/bin/ruby | |
# This program adds up the numbers 1 through 10,000 using 100 threads. | |
# Challenge A: Does this solution have any problems? If so, explain. | |
# Challenge B: Propose an alternate implementation that does not use threads. | |
require "thread" | |
mt_result = 0 |
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
#!/usr/bin/ruby | |
# This program adds up the numbers 1 through 10,000 using 100 threads. | |
# Challenge A: Does this solution have any problems? If so, explain. | |
# Challenge B: Propose an alternate implementation that does not use threads. | |
n = 10000 | |
p (1..n).reduce(:+) # by iteration |
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 KeyMetrics | |
def initialize(date, day) | |
@@data = Metric.last(source: 'all', start_date: date, end_date: date) | |
p 'DATA' | |
p @@data | |
end | |
def find | |
{ | |
visits: @@data.visits.to_s, | |
revenue: CURRENCY + sdlw_data.transaction_revenue.to_s |
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
Ramaze::Cache.options.session = Ramaze::Cache::MemCache.using( | |
compression: false, | |
username: ENV['MEMCACHE_USERNAME'], | |
password: ENV['MEMCACHE_PASSWORD'], | |
servers: ENV['MEMCACHE_SERVERS'].split(','), | |
) |
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
#!/usr/bin/ruby | |
def ask(question, retries = 3) | |
fail "Failed to get user input" if retries <= 0 | |
print question | |
if got = gets | |
got.strip | |
else |
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
require 'tempfile' | |
def check_authorized_keys(keyfile) | |
valid = false | |
File.foreach(keyfile).map do |line| | |
if line =~ /^ssh/ | |
Tempfile.open('key') do |keyfile| | |
keyfile.write(line) | |
keyfile.flush | |
@result = %x[echo #{keyfile.path} | ssh-keygen -l 2>&1] |
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
module ProjectReader | |
module_function | |
# finds keys in .erb files from a given directory | |
def find_keys(path) | |
keys = Dir[File.join(path, '**/*.erb')].map{|path| | |
extract_keys(path) | |
}.flatten | |
unless keys.empty? |
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
test = Hash.new | |
test.class == Hash #=> true | |
case test.class.to_s | |
when "Hash" | |
true | |
else | |
false | |
end | |
end #=> true |
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
def higher_order | |
lambda{|arg| arg + yield(5) } | |
end | |
foo = lambda{|arg| arg + 1 } | |
higher_order(&foo).(3) # => 9 |
NewerOlder