Skip to content

Instantly share code, notes, and snippets.

Jim OKelly thatrubylove

Block or report user

Report or block thatrubylove

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@thatrubylove
thatrubylove / lambda_fun.rb
Created Nov 22, 2013
lambda example for 2013-11-20-welcome-to-ruby-love-a-blog-where-we-love-ruby
View lambda_fun.rb
# 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)
View 6.rb
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 Dec 21, 2013
Interesting results from benchmarking
View benchmarks.json
[
{: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 Dec 22, 2013
Array vs LinkedList show down
View CPU Bench
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 Mar 28, 2014
Functional/OO Deck of Cards in Ruby - Start of a series of articles for rubylove.io
View card_deck.rb
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 Apr 1, 2014
Polymorphism over conditionals
View poly-over-cond.rb
# 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 Apr 2, 2014
one liner magic - but it is primitive obsessed with a string
View string_primitive_deck.rb
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 Aug 29, 2015
tdding-patshaughnessys-ask-dont-tell
View example-1.rb
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 Apr 16, 2014
tdding-patshaughnessys-ask-dont-tell - example-2.rb
View 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 Apr 16, 2014
tdding-patshaughnessys-ask-dont-tell-example-3.rb
View 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
You can’t perform that action at this time.