Skip to content

Instantly share code, notes, and snippets.

@vrinek
vrinek / nested_timers.rb
Last active August 29, 2015 13:57
Dead-simple nested timers for Ruby
class MyTimerClass
attr_reader :timers
def initialize
@timers = {}
@parent_timer = nil
end
def clock!(name, &block)
@timers[name] ||= 0
require 'active_support/hash_with_indifferent_access'
require 'minitest/autorun'
def sum(a: 0, b: 0)
a + b
end
class BugTest < MiniTest::Unit::TestCase
def setup
@normal_hash = {a: 18, b: 24} # just a hash
@vrinek
vrinek / post.md
Last active August 29, 2015 14:13
Ruby 2.2 - Warning: circular argument reference

Ruby 2.2.0 now warns against a circular argument reference. It is pretty simple to demonstrate and pretty difficult to explain so I'll just present some code snippets.

The first example shadows a function (which I have encountered in one of the open-source projects I'm involved with):

def foo
  42
end

def bar(foo = foo)
irb(main):065:0> a = "雷"
"雷"
irb(main):066:0> b = a.unpack("C*").pack("C*")
"\xE9\x9B\xB7"
irb(main):067:0> a == b
false
irb(main):068:0> a.bytes == b.bytes
true
irb(main):069:0> a == b.unpack("U*").pack("U*")
true
@import url("../rust.css");
body {
max-width:none;
}
@media only screen {
#toc {
position: absolute;
left: 0px;
@vrinek
vrinek / avatar.rb
Created June 25, 2009 12:02
attachment_fu custom error messages
class Avatar < ActiveRecord::Base
has_attachment :content_type => :image,
:storage => :file_system,
:max_size => 500.kilobytes,
:resize_to => 'c128x128',
:thumbnails => {:tiny => 'c24x24', :small => "c48x48"}
validates_as_attachment
after_validation :greeklize_attachment_errors
<tr class="marked">
<td colspan="5"><pre><a name="line6"></a>6 def toc_for(article)</pre></td>
</tr>
<tr class="uncovered">
<td colspan="5"><pre><a name="line7"></a>7 '<ul id="toc_links">' + article.toc.collect{ |h5|</pre></td>
</tr>
<tr class="uncovered">
<td colspan="5"><pre><a name="line8"></a>8 '<li>' + link_to(h5[:title], url_for_article(article) + '?article_page=' + h5[:page].to_s + '#' + h5[:id]) + '</li>'</pre></td>
</tr>
<tr class="uncovered">
# used for paginations (Model.paginate :page => page_from_params)
# it just makes the WillPaginate::InvalidPage errors go away
def page_from_params
(p = params[:page].to_i) > 0 ? p : 1
end
$$('a[data-remote=true]').each(function(a){
a.onclick = function(){
href = a.getAttribute('href');
url = href.split('?')[0];
params = href.split('?')[1];
newParam = encodeURI(eval(a.getAttribute('data-with')));
if(params){
params = params.split('&');
def all_controller_actions(controller)
(controller.ancestors[1, controller.ancestors.index(ApplicationController)] + [Object]).inject(controller.new.methods) do |methods, ancestor|
methods -= ancestor.new.methods unless ancestor.class == Module
methods
end.reject{|m| m =~ /^_/}.sort.map(&:to_sym)
end