Skip to content

Instantly share code, notes, and snippets.

View thatrubylove's full-sized avatar

Jim OKelly thatrubylove

View GitHub Profile
@thatrubylove
thatrubylove / lambda_fun.rb
Created November 22, 2013 18:47
lambda example for 2013-11-20-welcome-to-ruby-love-a-blog-where-we-love-ruby
# Functional coding in Ruby - better than chunky bacon
print_to_console = ->(msg) { puts msg }
print_to_motd_file = ->(msg) { File.write("motd.txt", msg) }
# Display the "message of the day" via the selected printer
motd = ->(printer) {
msg = ["Hello World,\n",
" Sincerely,\n",
" thatrubylove"].join
printer.(msg)
@thatrubylove
thatrubylove / 6.rb
Last active December 29, 2015 07:29
Solution for problem #6 @ http://projecteuler.net/problem=6
sum = ->(num_list) { num_list.reduce(:+) }
square = ->(number ) { number * number }
squares = ->(num_list) { num_list.map {|num| square.(num) } }
sum_squares = ->(num_list) { sum.(squares.(num_list)) }
square_sum = ->(num_list) { square.(sum.(num_list)) }
gem 'minitest'
require 'minitest/autorun'
describe "sum_squares" do
@thatrubylove
thatrubylove / benchmarks.json
Created December 21, 2013 19:28
Interesting results from benchmarking
[
{:type=>"Functional::Lists::SinglyLinked", :operation=>"#plus", :sample_size=>10000, :benchmark=>0.116519289},
{:type=>"Functional::Lists::SinglyLinked", :operation=>"#plus", :sample_size=>50000, :benchmark=>0.557987583},
{:type=>"Functional::Lists::SinglyLinked", :operation=>"#plus", :sample_size=>750000, :benchmark=>18.900261197},
{:type=>"Functional::Lists::SinglyLinked", :operation=>"#plus", :sample_size=>2000000, :benchmark=>145.656489316}
],
[
{:type=>"Array", :operation=>"#plus", :sample_size=>10000, :benchmark=>1.640012864},
{:type=>"Array", :operation=>"#plus", :sample_size=>50000, :benchmark=>5.108800796},
{:type=>"Array", :operation=>"#plus", :sample_size=>750000, :benchmark=>71.655338726},
@thatrubylove
thatrubylove / CPU Bench
Created December 22, 2013 19:18
Array vs LinkedList show down
Rehearsal -----------------------------------------------------
array#push (100) 0.000000 0.000000 0.000000 ( 0.000013)
list#push (100) 0.000000 0.000000 0.000000 ( 0.000078)
-------------------------------------------- total: 0.000000sec
user system total real
array#push (100) 0.000000 0.000000 0.000000 ( 0.000012)
list#push (100) 0.000000 0.000000 0.000000 ( 0.000044)
Rehearsal ------------------------------------------------------
array#push (1000) 0.000000 0.000000 0.000000 ( 0.000084)
@thatrubylove
thatrubylove / card_deck.rb
Created March 28, 2014 18:08
Functional/OO Deck of Cards in Ruby - Start of a series of articles for rubylove.io
Rank = Struct.new(:rank, :value) do
def to_s; rank; end
end
Card = Struct.new(:rank, :suit) do
def to_s; "#{rank} of #{suit}"; end
end
class Deck
SUITS = %w(hearts clubs spades diamonds)
@thatrubylove
thatrubylove / poly-over-cond.rb
Created April 1, 2014 18:18
Polymorphism over conditionals
# Conditional reach-around
User = Struct.new(:name, :role) do; end
def greet(user)
if user.name && user.role
"Hello, I am an #{user.role} user. My name is #{user.name}."
elsif user.name
"Hello, my name is #{user.name}."
else
@thatrubylove
thatrubylove / string_primitive_deck.rb
Created April 2, 2014 08:48
one liner magic - but it is primitive obsessed with a string
DECK = %w[Ace 2 3 4 5 6 7 8 9 10 Jack Queen King].product(%w[Clubs Diamonds Hearts Spades]).map {|rank, suit| "#{rank} of #{suit}" }
@thatrubylove
thatrubylove / example-1.rb
Last active August 29, 2015 13:59
tdding-patshaughnessys-ask-dont-tell
require 'minitest/autorun'
describe "#lines_after_word(lines, target)" do
let(:file) { File.expand_path("../innisfree.txt", __FILE__) }
let(:lines) { File.readlines(file) }
it "must return an empty array if no lines include the target word" do
lines_after_word(lines, "hola").must_equal []
end
end
@thatrubylove
thatrubylove / example-2.rb
Created April 16, 2014 02:38
tdding-patshaughnessys-ask-dont-tell - example-2.rb
def lines_after_word(lines, target)
index = lines.find_index {|line| line =~ /#{target}/ }
index.nil? ? [] : ""
end
@thatrubylove
thatrubylove / example-3.rb
Created April 16, 2014 02:41
tdding-patshaughnessys-ask-dont-tell-example-3.rb
require 'minitest/autorun'
describe "#lines_after_word(lines, target)" do
let(:lines) { File.readlines(File.expand_path("../innisfree.txt", __FILE__)) }
it "must return all the lines if the first line includes the target word" do
lines_after_word(lines, "Innisfree").count.must_equal lines.count
end
end