Skip to content

Instantly share code, notes, and snippets.

@tehprofessor
tehprofessor / promotion_with_flatuikit.rb
Last active December 18, 2015 01:29
RubyMotion Example: Promotion Screen using Grouper's FlatUIKit w/ navigation bar
class RootScreen < ProMotion::Screen
title "Bienvenue"
def will_appear
UIBarButtonItem.configureFlatButtonsWithColor([89, 91, 91].uicolor, highlightedColor: [64, 65, 65].uicolor, cornerRadius: 2.0)
set_navigation_appearnce!
set_attributes self.view, {
backgroundColor: [222, 226, 225].uicolor
}
@label ||= UILabel.alloc.initWithFrame(CGRectZero)
@label.text = "Application"
@tehprofessor
tehprofessor / model.rb
Last active December 18, 2015 12:39
DataMapper won't save/update object if called in #each block I'm sure I'm doing something übern00b here (as I am a DM n00b).
instances = Model.all
# If I try to perform this on all the instances, all return true but the changes are not persisted.
instances.each do |instance|
desc = instance.description
desc.gsub!(/some\swords/, '')
instance.update(:description => desc) # doesn't work
instance.description = desc
instance.save # doesn't work and returns true.
@tehprofessor
tehprofessor / backgrounding.rb
Last active December 18, 2015 17:18
Parallelism w/ Use Case and Example
# Let's say you have a collection of users you want to email once a week.
# Now using Sidekiq, it's easy to move this off the main thread, so that you're not blocking
# incoming requests to the Rails application.
#
#Chances are you have a worker that looks like the EmailUsersWorker below.
#
class EmailUsersWorker
include Sidekiq::Worker
@tehprofessor
tehprofessor / git-changelog.sh
Last active December 19, 2015 19:09
Git-Changelog Installer (only tested on Debian 7.0 and Mac OS X 10.8)
#!/bin/bash
unamestr=`uname`
platform_path='unknown'
if [[ "$unamestr" == "Linux" ]]; then
platform_path='/usr/bin'
if [[ $UID != 0 ]]; then
echo "Please run this script with sudo:"
echo "sudo $0 $*"
exit 1
@tehprofessor
tehprofessor / string.rb
Created September 23, 2013 00:44
Handy little shortcut for OpalRB, adds #to_element method (and #to_e via an alias) to String.
class String
def to_element
Element.find("#{self}")
end
alias :to_e :to_element
end
# Now you can do:
el = "#cool-stuff".to_e
# vs
@tehprofessor
tehprofessor / nsgradient_example.rb
Last active August 29, 2015 13:56
NSGradient.alloc.initWithColorsAndLocations -- RubyMotion (OS X)
def make_stepped_gradient
@grad = NSGradient.alloc.initWithColorsAndLocations(start_color, 0.0, middle_color, 0.015, end_color, 1.0, nil)
# Explodes with:
# 2014-02-23 10:52:01.323 my_app[86365:303] -[__NSCFNumber colorUsingColorSpace:]: unrecognized selector sent to instance 0x7f8764169b20
# 2014-02-23 10:52:01.416 my_app[86365:303] -[__NSCFNumber colorUsingColorSpace:]: unrecognized selector sent to instance 0x7f8764169b20
end
def start_color
@start_color ||= BW.rgba_color(100,100,100,1.0)
end
def middle_color
import scala.util._
import java.security.SecureRandom
import java.security.MessageDigest
/*
* Generates a Bearer Token with a length of
* 32 characters (MD5) or 64 characters (SHA-256) according to the
* specification RFC6750 (http://tools.ietf.org/html/rfc6750)
*
* Uniqueness obtained by by hashing system time combined with a
@tehprofessor
tehprofessor / GetOrElse.swift
Last active August 29, 2015 14:23
GetOrElse -- Swift
//
// GetOrElse.swift
//
// Requires Swift 2.0 for protocol extensions
//
import Foundation
protocol GetOrElse {
func getOrElse<T>(block block: () -> T) -> T
@tehprofessor
tehprofessor / nil_or_else_example.rb
Created February 8, 2016 06:07
Ruby 2.3's safe navigation operator allows for a pretty nice "or else" construct.
module OrElse
module ObjectImplementation
def or_else(_)
self
end
end
module NilClassImplementation
def or_else(val)
val
end
@tehprofessor
tehprofessor / .brew-fix.sh
Created May 24, 2018 20:15
Fixes Homebrew's annoying upgrade/update behaviour, will break `update --merge` functionality but quite honestly, IDGAF.
brew() {
local cmd=$1; shift
if test "$cmd" = 'upgrade' || test "$cmd" = 'update'; then
"__brew_update_or_upgrade" "$@"
else
command brew "$cmd" "$@"
fi
}
__brew_update_or_upgrade() {