Skip to content

Instantly share code, notes, and snippets.

@Bodacious
Bodacious / README.md
Last active May 13, 2016 16:46
How to find an average value, without having to store every data point

How to find an average value, without having to store every data point

This was a solution to a problem I was trying to solve on my webiste: how can I arrive at an average value over time, when new data is being provided, without having to store every datum.

I wrote this code to prove to myself that I wasn't crazy, and that this was indeed a valid way to calculate the mean.

@Bodacious
Bodacious / user.rb
Last active February 2, 2016 17:51
Sometimes I don't know why I bother writing comments in Ruby code...
# It's the class for users, silly.
class User
# Ummm, initializes a new user
def initialize(args)
@first_name = args[:first_name]
@last_name = args[:last_name]
end
# :|
@Bodacious
Bodacious / integer.rb
Created December 9, 2015 19:09
Another approach to fibonacci in Ruby
class Integer
# PHI - Some greek number
PHI = Rational(1836311903,1134903170).to_f
# Inverse of 1/PHI
I_PHI = -Rational(1, PHI).to_f
# Square root of 5
SQRT_5 = 5 ** 0.5
@Bodacious
Bodacious / fibonacci.rb
Created December 9, 2015 18:42
Method to return the nth value of the Fibonacci sequence
# PHI - Some greek number
PHI = Rational(610,377).to_f
# Negative inverse of PHI
I_PHI = -Rational(1, PHI).to_f
# Square root of 5
SQRT_5 = 5 ** 0.5
# Fibonacci value for given number x
@Bodacious
Bodacious / .gitconfig
Created December 4, 2015 15:40
My git config file
[diff]
tool = diff
algorythm = minimal
[color]
branch = auto
diff = auto
status = auto
interactive = auto
ui = true
pager = true
@Bodacious
Bodacious / results.txt
Last active October 22, 2015 10:54
Most efficient way of returning a constant string from a Ruby method
Rehearsal -------------------------------------------------
with freez 1.490000 0.010000 1.500000 ( 1.493176)
with constant 1.500000 0.000000 1.500000 ( 1.499597)
with new 2.060000 0.000000 2.060000 ( 2.064578)
---------------------------------------- total: 5.060000sec
user system total real
with freez 1.470000 0.000000 1.470000 ( 1.480206)
with constant 1.500000 0.000000 1.500000 ( 1.510912)
with new 2.030000 0.000000 2.030000 ( 2.038113)
@Bodacious
Bodacious / namespaceable_controller.rb
Last active October 20, 2015 10:35
Creating a "namespace" for Rails controllers
module NamespaceableController
extend ActiveSupport::Concern
included do
helper_method :namespace
end
private
@Bodacious
Bodacious / Explanation.md
Last active August 29, 2015 14:22
Ruby: Passing Method objects into methods that accept blocks is SLOWER than procs

Explanation

So I thought I was being clever, writing my Rails controller actions like this:

def create
  set_post_from_post_id
  set_comment_as_new
  respond_to do |format|
    comment.save
@Bodacious
Bodacious / benchmark.rb
Last active August 29, 2015 14:21
Ruby Benchmark: What's the fastest way to check the start of a String in Ruby?
require 'benchmark'
TIMES = 1000000
STRING = "this is a long and bad-ass string"
Benchmark.bmbm do |test|
test.report("Index") do
TIMES.times { STRING.index("this is") }
@Bodacious
Bodacious / multiple_conditionals.rb
Created May 13, 2015 12:28
When you should nest multiple conditionals in Ruby
def should_nest_multiple_conditionals?
x = rand(10).to_i
if Time.now.strftime("%A") != "Friday"
if current_client.is_large_faceless_conglomerate?
return true
elsif Time.now.strftime("%A") == "Friday"
return true
end
elsif LocalWeather.new("Edinburgh").sunny?