This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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}" | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| begin | |
| raise | |
| rescue => e | |
| logger.error e.message | |
| e.backtrace.each { |line| logger.error line } | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # & 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # YIELD - ARGUMENTS | |
| def call_this_block | |
| yield "tweet" | |
| end | |
| call_this_block { |myarg| puts myarg.upcase } # => TWEET | |
| Timeline | |
| def each |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |