Skip to content

Instantly share code, notes, and snippets.

@rwz
rwz / batshitcrazy.rb
Created June 21, 2012 11:45
control class methods from instance? easy!
class MyClass
attr_accessor :klass_name
def initialize(klass_name)
self.klass_name = klass_name
end
def self.name
to_s
end
@rwz
rwz / gist:3281979
Created August 7, 2012 05:35
Rendering Jbuilder templates from within model
# this is the easiest way to use partials from model I could find so far
class Entry < AR::Base
def as_json
context = ApplicationController.new.view_context
context.render('/api/entries/entry.json', entry: self)
end
end
str = "some file name.with dots.html.markdown"
extensions = str[/(?:\.\w+)+$/]
extensions ? [ str.sub(extensions, ''), extensions[1..-1].split('.') ] : [ str ]

ruby-1.9.3-p392 cumulative performance patch for rbenv

(I guarantee nothing. No warranty I am not responsible blah blah blah. Seems to work great for me so far. Thanks to Tyler Bird who I forked this from.)

This installs a patched ruby 1.9.3-p392 with the railsexpress patchsets: https://github.com/skaes/rvm-patchsets

Requirements

@rwz
rwz / gist:7636420
Created November 25, 2013 04:35
rbenv install 2.0.0-p353
/var/folders/50/nmb34mx94js1fd1z_s5fl8pr0000gn/T/ruby-build.20131124231727.32309 ~
HTTP/1.1 200 OK
Content-Type: binary/octet-stream
Content-Length: 13572794
Connection: keep-alive
Date: Sat, 23 Nov 2013 04:24:40 GMT
Last-Modified: Fri, 22 Nov 2013 05:12:40 GMT
ETag: "78282433fb697dd3613613ff55d734c1"
Accept-Ranges: bytes
Server: AmazonS3
@rwz
rwz / bm.rb
Created January 3, 2014 20:49
I18N east slavic pluralization benchmark
require "benchmark/ips"
require "set"
OLD_CODE = ->(n){
mod10 = n % 10
mod100 = n % 100
if mod10 == 1 && mod100 != 11
:one
elsif [2, 3, 4].include?(mod10) && ![12, 13, 14].include?(mod100)
@rwz
rwz / my_hash.rb
Last active January 2, 2016 09:39
An interesting Ruby problem
class MyHash < Hash
undef_method :fetch
def fetch(*)
# your code goes here
end
end
require "rspec/autorun"
@rwz
rwz / gist:14c0d8a5187b69922bb4
Last active January 28, 2019 11:42
Custom JSON types in AS 4.1+
# config/initializers/lol_json.rb
module ActiveSupport
module JSON
module Encoding
class JSONGemEncoder
BYPASS_JSONIFY = Set.new
alias_method :original_jsonify, :jsonify
@rwz
rwz / README.md
Created September 26, 2014 00:40
A form-object solution of my wet dreams.

Let's call it AdequateForm for now.

class SignUpForm < AdequateForm
  attribute :first_name
  attribute :last_name
  attribute :birthdate, :date
  
  validates :first_name, :birthdate, presence: true
end
// look ma, no jquery
// assuming that you have
// <a data-komments-permalink="/foobar">comments count goes here</a>
// somewhere in your HTML
function _kmtsLoaded(){
var links = [].slice.apply(document.querySelectorAll("a[data-komments-permalink]"));
links.forEach(function(link){