Skip to content

Instantly share code, notes, and snippets.

View bil-bas's full-sized avatar

Bil Bas bil-bas

View GitHub Profile
@bil-bas
bil-bas / gist:5939750
Last active December 19, 2015 10:19 — forked from anonymous/gist:5939682
puts 'What is the name and path of the file?'
filename = gets.chomp
text = File.read(filename)
freqs = text.scan(/[a-zA-Z]+/).each.with_object(Hash.new(0)) { |word, freqs| freqs[word] += 1 }
freqs = freqs.sort_by {|x,y| -y }
freqs.each {|word, freq| puts "#{word} #{freq}" }
module Collatz
class << self
attr_reader :lengths
def length_recursive(current, counter=0)
counter += 1
if current == 1
counter
elsif @lengths.key? current
@lengths[current] + counter # Take advantage of already working out the length from this number.
@bil-bas
bil-bas / foo_spec.rb
Last active December 15, 2015 21:29 — forked from arwagner/gist:5289895
def foo1 obj
obj.bar(1)
obj.bar(2)
end
def foo2 obj
obj.bar(2)
end
describe "foo1" do
@bil-bas
bil-bas / ruby_array_insert_every.rb
Last active December 14, 2015 14:58 — forked from pmarreck/ruby_array_insert_every.rb
Yay for ugly, inefficient one-liners!
# Using insertion
class Array
# Insert into the array.
def insert_every!(skip, str)
(skip...size).step(skip).with_index {|n, i| insert(n + i, str) }
self
end
# Create a new array and insert into it.
def insert_every(skip, str)
str = 'foo,bar,bar-foo,baz^key=value^key1=value1,bazfoo'
new_str = Hash[str.split(',').map{|s| k,*v = s.split('^'); [k.to_sym,v.empty? ? nil : Hash[v.map {|s|s.split('=')}.inject({}){|c,(k,v)| c[k.to_sym] = v; c}]]}] if str
@bil-bas
bil-bas / rubymas.rb
Last active December 10, 2015 14:39 — forked from anonymous/rubymas.rb
Overly show-offy implementation, in Ruby, of @syntacticsugar's Twelve Days of Christmas in Clojure.
class Fixnum
def ordinal
%w[first second third fourth fifth sixth seventh eighth ninth tenth eleventh twelfth][self - 1]
end
# Use #to_word rather than #to_s, so it doesn't break any other printing of numbers.
def to_word
%w[one two three four five six seven eight nine ten eleven twelve][self - 1]
end
end
@bil-bas
bil-bas / gist:3959439
Last active October 12, 2015 02:47 — forked from syntacticsugar/gist:3936358
Filter even numbers in a list; fork it for maximum fun!
#these are meant to be run in a REPL, and the java one in beanshell of something of the sort:
#ruby
[1,2,3,4].select &:even?
#python
[x for x in [1,2,3,4] if not x%2]
#or, more norvingly
filter(lambda x: not x%2, [1,2,3,4])
#clojure
@bil-bas
bil-bas / parser.rb
Created September 9, 2012 17:00 — forked from andrewhl/parser.rb
def parse(page)
table = page.at("table.grid")
rows = table.search("tr").to_a[1..-1]
rows.each.with_object [] do |row, rows|
row = parse_row row.search("td").map(&:text)
rows << row if row
end
end
def parse_row(row)
@bil-bas
bil-bas / wrap_module.rb
Created August 22, 2012 00:46 — forked from drewdeponte/gist:3420790
Require inside module try #1
# code.rb contains:
# class Frog
# end
module MyModule
class_eval File.read("./code.rb"), "code.rb", 1
end
p MyModule::Frog # Will succeed.
p Frog # Will fail.
@bil-bas
bil-bas / gist:3419649
Created August 21, 2012 21:38 — forked from anonymous/gist:3419574
Traffic CSV for nullsign
require 'csv'
# Read the list of hosts.
hosts = {}
CSV.open('./datafile.csv', 'r') do |fields|
hosts[fields[0]] = { :location => fields[1], :device_type => fields[3] }
end
# Find out the date to report on.
report_file = './report.csv'