Skip to content

Instantly share code, notes, and snippets.

@Bodacious
Bodacious / base.rb
Created August 20, 2012 17:56
An "ActiveRecord" library for RubyMotion
module ActiveRecord
class Base
COLUMN_TYPES_MAPPING = {
:int => 'INTEGER',
:long => 'INTEGER',
:longLongInt => 'BIGINT',
:bool => 'BOOLEAN',
:double => 'DECIMAL',
@Bodacious
Bodacious / MethodMissing.rb
Created November 23, 2010 13:07
Example of how to rewrite method_missing and respond_to? in Ruby
class CarPart
# does the method name end in _price ?
def ghost_method_condition(name)
!!name.to_s[/_price$/]
end
# rewrite method missing if the method matches ghost_method_condition
def method_missing(name, *args, &block)
super unless ghost_method_condition(name)
@Bodacious
Bodacious / random_password.rb
Last active June 5, 2017 11:17
Random password generator in Ruby on Rails
# Examples:
#
# @password = RandomPassword.password
#
class RandomPassword
class << self
delegate :password, to: new
end
@Bodacious
Bodacious / app_delegate.rb
Created February 7, 2017 12:47
Empty app delegate
class AppDelegate
def window
@window ||= UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)
end
def application(application, didFinishLaunchingWithOptions:launchOptions)
window.rootViewController = UIViewController.alloc.init
window.makeKeyAndVisible
true
@Bodacious
Bodacious / app_delegate.rb
Created February 7, 2017 12:37
RubyMotion Hello World example.
class AppDelegate
def hello_world_label
@hello_world_label ||= begin
frame = CGRectMake(20,200,280,40)
label = UILabel.alloc.initWithFrame(frame)
label.text = "Hello world"
label.textColor = UIColor.whiteColor
label.textAlignment = UITextAlignmentCenter
label
@Bodacious
Bodacious / example 1.rb
Created October 28, 2016 09:38
Adding gender-specific messages to your rails app with genderize
# Since Sarah is the object of this sentence
"Sarah is a new member, why not send #{@sarah.object} a message?"
@Bodacious
Bodacious / sass.sass
Created October 27, 2016 17:00
SASS script for creating quick colour pallette
$hue: 173;
$first-color: hsl($hue, 100%, 50%);
$second-color: complement($first-color);
.first-color {
background: $first-color;
}
.second-color {
background: $second-color;
}
@Bodacious
Bodacious / pesapal.rb
Last active October 25, 2016 17:49
PesaPal OpenAuth integration (not working yet)
require 'oauth'
require 'uri'
key = # my Consumer Key (sandbox)
sec = # my Consumer Secret (sandbox)
SITE_URL = 'https://demo.pesapal.com'
# An XML string of param data to include with our request
RAW_XML = <<-XML
@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
# :|