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 PokerGame | |
| include Enumerable | |
| include Comparable | |
| attr_reader :hands, :deck | |
| RANKS = %w{ 2 3 4 5 6 7 8 9 T J Q K A } | |
| SUITS = %w{ S H D C } | |
| def initialize(hands, cards) | |
| @deck = RANKS.product(SUITS).map(&: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
| def events_all_accounts(time) | |
| self.accounts.flat_map do |account| | |
| account.events.send(time, account.now).where(:client_id => self.id) | |
| end.uniq.sort_by { |a| a.start_time } | |
| end | |
| private :events_all_accounts | |
| # all upcoming events for a client | |
| def future_events_all_accounts | |
| events_all_accounts(:in_future) |
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
| primes = [2] | |
| current_number = 3 | |
| while primes.length < 10 | |
| primes.push(current_number) unless primes.any? { |p| current_number % p == 0 } | |
| current_number += 2 | |
| end | |
| puts primes |
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
| device_from_filename() { | |
| # canonicalize filename | |
| local realpath="$(readlink -e "$1")" | |
| [[ ! -e $realpath ]] && return 1 | |
| # read /etc/mtab into associative array mtab | |
| local -A mtab | |
| local device mountpoint rest | |
| while read device mountpoint rest; do |
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
| TOKEN_PROCESSORS = [] | |
| def processor(regex, &proc) | |
| TOKEN_PROCESSORS << [regex, proc] | |
| end | |
| # Finds a word followed by any value | |
| processor /\A([A-z]\w*)\s*([^\n]*)?/ do |chunk, matches| | |
| matches[0].upcase! |
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
| attr_writer :full_name | |
| def full_name | |
| @full_name || "#{@first_name} #{@last_name}" | |
| 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
| # encoding: utf-8 | |
| require 'rubygems' | |
| require 'yaml' | |
| def maybe_gem(name) | |
| gem name | |
| rescue Gem::LoadError | |
| nil | |
| 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
| require 'benchmark' | |
| require 'set' | |
| array = (1..10000).to_a | |
| set = array.to_set | |
| probes = 10000.times.map { rand(20000) } | |
| Benchmark.bmbm do |bm| | |
| bm.report("Array") { probes.each { |p| array.include? p } } | |
| bm.report("Set") { probes.each { |p| set.include? p } } |
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
| triangle = Enumerator.new do |e| | |
| arr = [1] | |
| loop do | |
| e << arr.join(" ") | |
| arr = [1, *arr.each_cons(2).map { |x| x.inject(:+) }, 1] | |
| end | |
| end | |
| while (depth = gets.to_i) > 0 | |
| lines = triangle.take(depth) |
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 | |
| until (words = gets.chomp.split).empty? | |
| stack = [] | |
| words.each do |w| | |
| begin | |
| stack.push Float(w) | |
| rescue ArgumentError | |
| args = stack.pop(Float.instance_method(w).arity + 1) | |
| stack.push args.first.send(w, *args[1..-1]) |