Skip to content

Instantly share code, notes, and snippets.

@rwz
rwz / wtf.rb
Last active April 26, 2017 19:00
dynamic method definition in ruby
class MyClass
def static_yield(*)
yield if block_given?
end
define_method :dynamic_yield do |*|
yield if block_given?
end
define_method :dynamic_yield_fix do |*, &block|
@rwz
rwz / p5n.md
Last active October 24, 2021 15:09
persona 5 negotiatons

Codes:

  • L: Likes
  • D: Dislikes
  • M: Meh.

Depending on the persona for successful negotiation you need either 2 Like answers or 1 Like + 1 Meh.


my_string.scan(/[a-z\d]+/).each_with_object(Hash.new(0)){ |e, a| a[e] += 1 }
@rwz
rwz / throw-catch.rb
Created November 6, 2015 19:06
How ensure/return breaks throw/catch
def run_with_elapsed_time
time_at_start = Time.now
exception = nil
result = yield
rescue => e
exception = e
ensure
return [ result, exception, Time.now - time_at_start ]
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){
@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
@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 / 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 / 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 / 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