Skip to content

Instantly share code, notes, and snippets.

class Person
attr_accessor :first_name, :last_name
def whole_name
first_name + " " + last_name
end
end
p = Person.new
p.first_name = "David"
class A # top-level A
end
module M
module ClassMethods
def create_an_a
A.new # top-level A
end
end
@dblack
dblack / mint.rb
Last active December 13, 2015 22:59 — forked from Trevoke/mint.rb
# Forked from trevoke
require 'csv'
require 'yaml'
require 'rails/all'
require 'active_record'
require 'active_support'
require 'sqlite3'
@dblack
dblack / gist:3017739
Created June 29, 2012 12:50
weakref behavior
This is from the end of weakref.rb:
if __FILE__ == $0
# require 'thread'
foo = Object.new
p foo.to_s # original's class
foo = WeakRef.new(foo)
p foo.to_s # should be same class
ObjectSpace.garbage_collect
ObjectSpace.garbage_collect
@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
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>
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 }
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"]}>
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
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?'