Skip to content

Instantly share code, notes, and snippets.

@javan
javan / string-to-proc.md
Created August 7, 2014 22:05
Ruby String#to_proc
class String
  def to_proc
    Proc.new { |n| self.split(".").inject(n) { |n, method| n.send(method) } }
  end
end
>> Person.first(2).map(&"name.downcase.reverse.capitalize")
class Document < ActiveRecord::Base
columns.select { |c| c.limit && c.type.in?([:string, :text]) }.each do |column|
validates_length_of column.name, maximum: column.limit, tokenizer: ->(string) { string.bytes }
end
end
diff --git a/actionpack/lib/action_dispatch/http/response.rb b/actionpack/lib/action_dispatch/http/response.rb
index 4379255..58bdf2a 100644
--- a/actionpack/lib/action_dispatch/http/response.rb
+++ b/actionpack/lib/action_dispatch/http/response.rb
@@ -371,7 +371,7 @@ module ActionDispatch # :nodoc:
end
def to_ary
- nil
+ @response.body_parts

IntegrityTest#test_"images on disk have no duplicates" [/Users/javan/Code/gemoji/test/integrity_test.rb:26]: These images share the same checksum: /emoji/unicode/25fc.png, /emoji/unicode/2b1b.png.

--

◼️ "BLACK MEDIUM SQUARE" Unicode: U+25FC U+FE0F, UTF-8: E2 97 BC EF B8 8F

⬛️

Trix.attributes.strike =
tagName: "span"
style: { "text-decoration": "line-through" }
inheritable: true
# UPDATE: I benchmarked parsing JSON and it's way way faster. I'm going to transition to requesting
# JSON from flickr and store the cached data as JSON too.
#
# With Rreset (http://github.com/javan/rreset/tree/master), I am caching some of the XML
# returned from Flickr API calls. Then, on subsequent hits, the cached XML is loaded and parsed
# using Hash.from_xml which is an implementation of XmlSimple. I was curious if it would be
# faster to first convert the XML to Yaml and parse that on cached hits instead. Yaml was the
# obvious winner. There are probably way faster XML parsing libraries out there,
# but I wanted to stick with the Rails defaults.
#
sudo port install ruby
# Install rubygems 1.2.0 because the memcached gem requires it (macport installs 1.3.0)
curl -O http://files.rubyforge.mmmultiworks.com/rubygems/rubygems-1.2.0.tgz
tar xzvf rubygems-1.2.0.tgz
cd rubygems-1.2.0
sudo ruby setup.rb
#memcached
wget http://www.danga.com/memcached/dist/memcached-1.2.6.tar.gz
<% if 1 == 1 # some comment %>
In ruby 1.8.6 you'll see me. In 1.8.7 you won't. It seems 1.8.7 doesn't like comments in erb tags like the one above.
<% end %>
class User < ActiveRecord::Base
# These are all boolean fields defined for this User model.
# As we add more email columns, and add them to this array,
# the named_scope below keeps up.
EMAIL_COLUMNS = [:email_new_market_alerts, :email_new_announcements, :email_stock_notices]
named_scope :unsubscribed, lambda { { :conditions => EMAIL_COLUMNS.inject({}) { |conditions, column| conditions[column] = false; conditions } } }
end
# Takes a collection of Stocks and moves the closed ones to the end
def closed_stocks_last(stocks)
open, closed = [], []
stocks.each do |stock|
if stock.closed?
closed << stock
else
open << stock
end