Skip to content

Instantly share code, notes, and snippets.

module PerActionMiddleware
# DUMMY MODULE, just to get the file loaded.
end
module ActionController
class Base
class << self
alias _call call
def call(env)
# To use this, put it in per_action_middleware.rb,
# add PerActionMiddleware to the class_eval at the end
# of base.rb, and add this line to action_controller.rb:
#
# autoload :PerActionMiddleware, 'action_controller/per_action_middleware'
#
# I don't like the dummy module thing but at least this seems to do
# the trick on the initializations.
module PerActionMiddleware
class Bar
attr_reader :hash
def method_missing(m, *args)
if m.to_s =~ /((set|get)_(.+))/
func =
case $2
when 'set' then lambda {|value| @hash ||= {}; @hash[$3] = value}
when 'get' then lambda {@hash ||= {}; @hash[$3]}
end
self.class.send(:define_method, $1, &func)
# An example of inject: deep_const_get.
# This versions of const_get works with nested constants
# (unlike regular const_get, which chokes on "::").
#
# This implementation does some output along the way to
# show you what's happening.
class Module
def deep_const_get(const)
segments = const.split("::")
1. Without the change to line 7 of scripts/rvm:
$ rvm default
$ ruby -v
ruby 1.9.2p0 (2010-08-18 revision 29036) [i386-darwin9.8.0]
$ sh
sh: /Users/dblack/.rvm/scripts/rvm: line 7: syntax error near unexpected token
`<'
sh: /Users/dblack/.rvm/scripts/rvm: line 7: `\grep -q '^rvm ()' < <( declare -f
) # Is RVM is a shell function?'
Why does the app always take the HTML/index.html.erb path when the link is clicked?
Does index.js.erb not work any more?
# in controller
def index
@page = Page.find(params[:page_id])
@ideas = @page.ideas
respond_to do |format|
format.js
format.html
ree-1.8.7-2010.02 :001 > res = Team.search { keywords "Mets" }
=> <Sunspot::Search:{:defType=>"dismax", :q=>"Mets", :rows=>30, :start=>0,
:fl=>"* score", :qf=>"name_text", :fq=>["type:Team"]}>
ree-1.8.7-2010.02 :002 > res.results
=> [#<Team _id: BSON::ObjectId('4dfa2262508c319700000001'), name: "Mets">]
ree-1.8.7-2010.02 :003 > Team.reindex(:batch_size => false)
=> {"responseHeader"=>{"QTime"=>137, "status"=>0}}
ree-1.8.7-2010.02 :004 > res = Team.search { keywords "Mets" }
=> <Sunspot::Search:{:defType=>"dismax", :q=>"Mets", :rows=>30, :start=>0,
:fl=>"* score", :qf=>"name_text", :fq=>["type:Team"]}>
def max_length_histogram(str)
Hash.new(0).tap do |hist|
str.scan(/((.)\2*)/) do |substring, letter|
hist[letter] = [ hist[letter], substring.size ].max
end
end
end
def longest_run(str)
max_length_histogram(str).max_by {|letter, n| n }
Update: I figured it out. I was loading the .js file twice.
In the view:
<%= link_to "Toggle comment", "#", :id => "toggle-comment" %>
<p id="comment" style="display:none;">Comment: <%= @scratchpad.comment %></p>
@dblack
dblack / gist:3012525
Created June 28, 2012 16:59
Behavior of take_while and drop_while without a block
I'm puzzled by the behavior of take_while and drop_while when called without
a block. They return enumerators -- that part I understand -- but I'm not
understanding how those enumerators behave.
Here's what I mean:
>> enum = [1,2,3,4,5].take_while
=> #<Enumerator: [1, 2, 3, 4, 5]:take_while>
>> enum.next
=> 1