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
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}" } |
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
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. |
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 foo1 obj | |
obj.bar(1) | |
obj.bar(2) | |
end | |
def foo2 obj | |
obj.bar(2) | |
end | |
describe "foo1" do |
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
# 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) |
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
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 |
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 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 |
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
#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 |
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 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) |
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
# 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. |
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 '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' |
NewerOlder