Skip to content

Instantly share code, notes, and snippets.

# called in main
pry(main)> local_variables
=> [:__, :_, :_dir_, :_file_, :_ex_, :_pry_, :_out_, :_in_, :_erbout]
# called in method with no local vars
pry(main)> def a; local_variables; end
=> nil
pry(main)> a
=> []
@stevenbristol
stevenbristol / gist:3504205
Created August 28, 2012 20:50
auto select timezone with javascript in rails
.sign_up_in_content
= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name))
= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f|
%span
= f.input :email, :input_html => {:placeholder => 'Email'}
%span
scope by_date, order("date desc")
scope stuff, -> (kind) {where("stuff = ?", kind)
scope pens, stuff("pen")
scope allans, stuff("allan").by_date
@stevenbristol
stevenbristol / procscope.rb
Created August 9, 2012 17:32
scope with proc usage
class User < ActiveRecord::Base
scope :hello, Proc.new { |param| param.blank? ? where("") : where("param = ?", param) }
end
User.hello(current_user_or_other_security_thig_or_somethng)
User.hello( "" )
User.hello
@stevenbristol
stevenbristol / proc.rb
Created August 9, 2012 17:18
Proc in scope
class User < ActiveRecord::Base
scope :hello_lamb, ->(param) { param.blank? ? where("") : where("param = ?", param) }
scope :hello_proc, Proc.new { |param| param.blank? ? where("") : where("param = ?", param) }
end
User.hello_proc
# User Load (0.5ms) SELECT * FROM users
@stevenbristol
stevenbristol / scope.rb
Created August 9, 2012 17:01
A class with a proc scope
class User < ActiveRecord::Base
scope :hello_lamb, ->(param) { param.blank? ? where("") : where("param = ?", param) }
scope :hello_proc, Proc.new { |param| param.blank? ? where("") : where("param = ?", param) }
end
User.hello_lamb "a"
# User Load (0.5ms) SELECT * FROM users WHERE (param = 'a')
#=> #<ActiveRecord::Relation:0x3fe6eedeb984>
@stevenbristol
stevenbristol / self2.rb
Created August 8, 2012 12:45
What is self?
class Thing
def self.log str
p str
end
def test_log
begin
self.log "does not work"
rescue Exception => e
p e
end
@stevenbristol
stevenbristol / mod.rb
Created August 6, 2012 14:00
typical module setup
module Mod
module ClassMethods
#put class methods here
end
module InstanceMethods
#put instnace methods here
end
def self.included(receiver)
@stevenbristol
stevenbristol / include.rb
Created August 6, 2012 13:35
include and extend using included
module Log
def Log.included cls
cls.extend Log #also works by passing self, but Log is less confusing
end
def log str
p str
end
end
class Thing
st_hash(const void *ptr, size_t len, st_index_t h)
{
const char *data = ptr;
st_index_t t = 0;
h += 0xdeadbeef;
#define data_at(n) (st_index_t)((unsigned char)data[(n)])
#define UNALIGNED_ADD_4 UNALIGNED_ADD(2); UNALIGNED_ADD(1); UNALIGNED_ADD(0)
#if SIZEOF_ST_INDEX_T > 4