Skip to content

Instantly share code, notes, and snippets.

View rbishop's full-sized avatar

Richard Bishop rbishop

View GitHub Profile
@rbishop
rbishop / count_between.rb
Last active December 10, 2015 18:08
Exercise: Count the numbers in an array between a given range Write a method count_between which takes three arguments as input: 1. An Array of integers 2. An integer lower bound 3. An integer upper bound count_between should return the number of integers in the Array between the two bounds, including the bounds It should return 0 if the Array i…
def count_between arr, lower, upper
return 0 if arr.length == 0 || lower > upper
return arr.length if lower == upper
range = (lower..upper).to_a
arr.select { |value| range.include?(value) }.length
end
@rbishop
rbishop / gist:4508218
Last active December 10, 2015 23:18
times table
def times_table number
multiplier = 1
while multiplier <= number
range = (1..number).to_a
row = range.map do |x|
x * multiplier
end
puts row.join(' ')
multiplier += 1
end
@rbishop
rbishop / repl.rb
Last active March 23, 2016 11:32
Minimal implementation of a REPL in Ruby. WAT?!
while true
line_num ||= 1
("%03d" % line_num + ' > ').display
code = gets
puts eval(code)
line_num += 1
end
@rbishop
rbishop / object_send.js
Created January 24, 2013 04:59
So, everything is an object eh, JavaScript? Then every object should have a send method! This is just for fun of course and I pray to not be punished by the gods for extending native prototypes. How fun is it to have a ton of dynamic method dispatching at your hands though?
Object.prototype.send = function(methodName) {
args = [].slice.call(arguments);
args.shift()
if (this.hasOwnProperty(methodName)) return this[methodName];
if (args.length > 1) {
return this[methodName].apply(this, args);
} else {
return this[methodName]();
@rbishop
rbishop / largest_prime_factor.rb
Created February 10, 2013 21:49
Euler problem #3
def largest_prime_factor num
factor = 2
product = 1
while product != num
factor += 1
if num % factor == 0
product *= factor
end
end
return factor
Rails.cache.fetch :group_id => group.id, :goal_id => self.id do
Post.find_by_sql(
"#{Post.where(:attachment_type => 'UserGoalLog').where(Post.arel_table[:attachment_id].in(user_goal_logs.collect(&:id))).to_sql}
UNION
#{Post.where(:attachment_type => 'UserGoal').where(Post.arel_table[:attachment_id].in(user_goals.collect(&:id))).to_sql}"
)
end
@rbishop
rbishop / gist:4974678
Created February 18, 2013 01:56
Fixes the issue where the fixed navigation menu in Twitter Bootstrap sits on top of your main div container.
// Add #main to the second div.container
@media (min-width: 720px) {
#main {
margin-top: 50px;
}
}
@rbishop
rbishop / concern.rb
Created October 3, 2013 23:54
I imagine this is due to how ActiveSupport::Concern handles dependency resolution? It's pretty surprising though because you can use most other Rails class methods (before_filter, helper_method, belongs_to, etc. etc.) in the included method's block.
module Surprisable
extend ActiveSupport::Concern
included do
attr_accessor :this_aint_gonna_work
end
end
module Wtfable
extend ActiveSupport::Concern
@rbishop
rbishop / decorator_perf.rb
Created November 20, 2013 00:40
Disclaimer: Yes, this is performance testing and Ruby. Obviously we choose Ruby for it's other greater qualities. Playing around with performance regarding the various ways you can implement Decorators in Ruby.
require 'benchmark'
require 'delegate'
require 'forwardable'
class Person
def initialize(name)
@name = name
end
def name
@rbishop
rbishop / carl_hewitt_actor_model.md
Last active April 3, 2024 06:58
Notes from Carl Hewitt on the Actor Model

Carl Hewitt on Actors

Actor - Fundamental unit of computation, a computation model - not just a form of concurrency

An Actor has three essential elements:

  • 1 - Processing - you have to get something done
  • 2 - Storage - you have to be able to remember things
  • 3 - Communication