Skip to content

Instantly share code, notes, and snippets.

@ilake
ilake / gist:4250924
Created December 10, 2012 14:42
Digest::MD5
[1] pry(main)> Digest::MD5.new.update('version-1')
=> #<Digest::MD5: e8444fc50e000a592c6f49872490349e>
[2] pry(main)> Digest::MD5.new.update('version-1').update('mini-2')
=> #<Digest::MD5: 9f328f25432758cd63ba0ddd38024364>
[3] pry(main)> Digest::MD5.new.update('version-1').update('mini-2').hexdigest
=> "9f328f25432758cd63ba0ddd38024364"
[4] pry(main)> Digest::MD5.hexdigest('version-1')
=> "e8444fc50e000a592c6f49872490349e"
@ilake
ilake / gist:4210583
Created December 5, 2012 00:13
assets pipeline
# For sprockets-2.1.3
# in lib/sprockets/asset.rb
# Asset class, initialize method
# just use path
@digest = environment.file_digest(pathname).hexdigest
puts "========== Assets::initialize:pathname #{pathname}"
puts "========== Assets::initialize:@digest #{@digest}"
@ilake
ilake / gist:4157952
Created November 27, 2012 23:33
trace
begin
raise
rescue => e
logger.error e.message
e.backtrace.each { |line| logger.error line }
end
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#button").click(function()  
// Do anything you want here
return false // cancel default action
);
});
</script>
@ilake
ilake / gist:4075750
Created November 15, 2012 00:06
Rubybits2 LEVEL 1: Proc, block, lambda
# & then proc object into block
# & also could then block into a proc object
# & 可以讓proc object 跟 block 切來切去
# Calling a method with & in front of a parameter
tweets.each(&printer) # turns a proc into block
# Defining a method with & in front of a parameter
def each(&block) # turns a block into a proc, so it can be assigned to parameter
@ilake
ilake / gist:4072530
Created November 14, 2012 14:46
LEVEL 6: BLOCKS
# YIELD - ARGUMENTS
def call_this_block
yield "tweet"
end
call_this_block { |myarg| puts myarg.upcase } # => TWEET
Timeline
def each
@ilake
ilake / gist:4070078
Created November 14, 2012 03:32
Rails config.assets.precompile setting to process all CSS and JS files in app/assets
config.assets.precompile << Proc.new { |path|
if path =~ /\.(css|js)\z/
full_path = Rails.application.assets.resolve(path).to_path
app_assets_path = Rails.root.join('app', 'assets').to_path
if full_path.starts_with? app_assets_path
puts "including asset: " + full_path
true
else
puts "excluding asset: " + full_path
false
@ilake
ilake / gist:4069539
Created November 14, 2012 01:05
LEVEL 5: MODULES
Image.ancestors # [Image, ImageUtils, Object, Kernel, BasicObject]
Image.included_modules # [ImageUtils, Kernel]
class Image
image = Image.new
image.extend(ImageUtils)
image.preview # an object is extending the module
end
# the module will not be available in other objects
@ilake
ilake / gist:4069075
Created November 13, 2012 23:16
LEVEL 4: ACTIVESUPPORT
array.from(4)
array.to(2)
array.in_groups_of(3)
array.split(2)
array.index('word')
apocalypse = DateTime.new(2012, 12, 21, 14, 27, 45)
apocalypse.advance(years: 4, months: 3, weeks: 2, days: 1)
apocalypse.tomorrow
apocalypse.yesterday
@ilake
ilake / gist:4062922
Created November 12, 2012 23:59
The 10 Most Underused ActiveRecord::Relation Methods
# http://blog.mitchcrowe.com/blog/2012/04/14/10-most-underused-activerecord-relation-methods/
1. Merge
class Account < ActiveRecord::Base
# ...
# Returns all the accounts that have unread messages.
def self.with_unread_messages
joins(:messages).merge( Message.unread )
end
end