This file contains 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 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 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 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]) |
This file contains 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 Enumerator | |
class Do < BasicObject | |
def initialize(enum) | |
@enum = enum | |
end | |
def method_missing(meth, *args, &blk) | |
@enum.each do |x| | |
x.send(meth, *args, &blk) | |
end |
This file contains 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 'java' | |
url = Java::JavaNet::URL.new("file:///usr/share/sounds/alsa/Front_Center.wav") | |
clip = Java::JavaApplet::Applet.newAudioClip(url) | |
clip.play | |
sleep 3 |
This file contains 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 fetch_ftp | |
ftp = Net::FTP.new(self.meta[:host]) | |
ftp.login(user = self.username, passwd = self.password) | |
buffer = "" | |
ftp.getbinaryfile(self.url) { |data| buffer << data } | |
ftp.quit() | |
MultiXml.parse(buffer) rescue nil |
This file contains 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
# grub-mkimage -d /usr/lib/grub/x86_64-efi -O x86_64-efi -p /grub -o /boot/EFI/boot/bootx64.efi part_gpt fat normal boot configfile | |
set color_normal=white/black | |
set color_highlight=black/white | |
set menu_color_normal=blue/black | |
set menu_color_highlight=black/blue | |
set timeout=1 | |
insmod efi_gop | |
set default=0 |
This file contains 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
BEGIN { | |
require "fileutils" | |
FileUtils.touch("run.old") | |
$stdout = $stderr = open("run","w") | |
} | |
END { | |
system("diff -u run.old run") | |
FileUtils.mv("run", "run.old", :force => true) | |
} |
This file contains 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 printgrid(table) | |
strings = table.map do |row| | |
row.map { |cell| cell.to_s } | |
end | |
column_widths = [] | |
strings.each do |row| | |
row.each_with_index do |cell,i| | |
column_widths[i] = [column_widths[i] || 1, cell.length + 2].max | |
end |