Skip to content

Instantly share code, notes, and snippets.

@RyanScottLewis
RyanScottLewis / parallelPprocessing.rb
Created November 18, 2012 17:47
Parallel processing
require 'thread'
require 'bundler/setup'
require 'system'
from, to = Queue.new, Queue.new
item_count = rand(100) + 100
thread_count = (System::CPU.count * 4)
(1..item_count).each { |item| from.push(item) }
module System
extend self
def cpu_count
return Java::Java.lang.Runtime.getRuntime.availableProcessors if defined? Java::Java
return File.read('/proc/cpuinfo').scan(/^processor\s*:/).size if File.exist? '/proc/cpuinfo'
require 'win32ole'
WIN32OLE.connect("winmgmts://").ExecQuery("select * from Win32_ComputerSystem").NumberOfProcessors
rescue LoadError
Integer `sysctl -n hw.ncpu 2>/dev/null` rescue 1
end
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>My Site</title>
<style type="text/less">
.clearfix:after {
content: ".";
@RyanScottLewis
RyanScottLewis / even.java
Created October 2, 2012 00:41
Even Number Printing
int limit = 100;
// moop through each number from 1 to the limit (100)
for(int i=1; i <= limit; i++){
// everything in this block of code will be ran for each number
// if the number is divisible by 2 then it is even
if (i % 2 == 0) {
// print out the number with a space after it
System.out.print(i + " ");
@RyanScottLewis
RyanScottLewis / setter_pattern.coffee
Created July 21, 2012 10:42 — forked from alexaivars/setter_pattern.coffee
Getter Setter patter for Coffeescript
Function::define = (prop, desc) ->
Object.defineProperty this.prototype, prop, desc
class GetterSetterTest
constructor: (@_obj = {}) ->
# 'obj' is defined via the prototype, the definition proxied through
# to 'Object.defineProperty' via a function called 'define' providing
# some nice syntactic sugar. Remember, the value of '@' is
# GetterSetterTest itself when used in the body of it's class definition.
@RyanScottLewis
RyanScottLewis / cacheable_attr.rb
Created June 25, 2012 05:55
Cacheable attributes
module CacheableAttr
def cacheable_attr(*attributes)
opts = { ttl: 60 }
opts = opts.merge(attributes.pop) if attributes.last.is_a?(Hash)
attributes.each do |attribute|
define_method attribute do
if !instance_variable_defined?("@#{attribute}_updated_at") || (opts[:ttl] != nil && Time.now >= instance_variable_get("@#{attribute}_updated_at") + opts[:ttl])
value = send("#{attribute}_uncached")
@RyanScottLewis
RyanScottLewis / _schema.rb
Created June 22, 2012 05:45
SpineJS User's Name helper + showing off getters/setters in Spine
ActiveRecord::Schema.define(:version => 00000000000000) do
create_table "users", :force => true do |t|
t.string "username", :default => "", :null => false
t.string "email", :default => "", :null => false
t.string "encrypted_password", :default => "", :null => false
# Devise fluff here...
t.string "first_name", :default => "", :null => false
/Users/Ryguy/.rvm/gems/ruby-1.9.2-p318@proctor/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:503:in `load_missing_constant': Expected /Users/Ryguy/Code/Ruby/Gems/proctor/app/models/client.rb to define Client (LoadError)
from /Users/Ryguy/.rvm/gems/ruby-1.9.2-p318@proctor/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:192:in `block in const_missing'
from /Users/Ryguy/.rvm/gems/ruby-1.9.2-p318@proctor/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:190:in `each'
from /Users/Ryguy/.rvm/gems/ruby-1.9.2-p318@proctor/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:190:in `const_missing'
from /Users/Ryguy/.rvm/gems/ruby-1.9.2-p318@proctor/gems/activesupport-3.2.3/lib/active_support/inflector/methods.rb:229:in `block in constantize'
from /Users/Ryguy/.rvm/gems/ruby-1.9.2-p318@proctor/gems/activesupport-3.2.3/lib/active_support/inflector/methods.rb:228:in `each'
from /Users/Ryguy/.rvm/gems/ruby-1.9.2-p318@proctor/gems/activesupport-3.2.3/lib/active_support/infle
@RyanScottLewis
RyanScottLewis / spec_helper.rb
Created May 1, 2012 10:47
Rspec: clear terminal before suites in OSX
# omitted...
RSpec.configure do |config|
# omitted...
config.before(:suite) do
if RUBY_PLATFORM =~ /darwin/
# Focus on Terminal
system %{osascript -e 'tell application "Terminal" to activate'}
# Send CMD+K to clear
ryguy@RyBook ~/Code/Ruby/Scratchpad/slop-test $ gem list slop
*** LOCAL GEMS ***
slop (3.1.1)
ryguy@RyBook ~/Code/Ruby/Scratchpad/slop-test $ cat test.rb
require 'slop'
opts = Slop.new do