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
| a = ["1", "2", "3", "4", "5"] | |
| a.map { |s| s.to_i } # Ruby 1.8 | |
| # => [1,2,3,4,5] | |
| a.map(&:to_i) # Ruby 1.9 | |
| # => [1,2,3,4,5] |
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
| a = [1,2,3,4,5] | |
| # Sum | |
| a.inject(&:+) | |
| # => 15 | |
| # Product | |
| a.inject(&:*) | |
| # => 120 |
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
| # Get total sum of the sizes of each word | |
| words = "Hello World, how are you today?".split(/\s/) | |
| words.map(&:size).inject(&:+) | |
| # => 26 | |
| # Validate numerous fields | |
| fields = ["foo", "bar", "tomato", "tomatoes", nil] | |
| valid = !fields.map(&:nil?).inject(&:|) |
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
| # Generate a poker hand | |
| faces = (2..10).to_a + %w(J Q K A) | |
| suits = %w(S C H A) | |
| suits.product(faces).sample(5).map(&:join) | |
| # => ["S7", "HA", "S8", "C4", "C8"] |
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
| $ ruby toproc.rb | |
| Rehearsal -------------------------------------------------- | |
| Method invoke 1.240000 0.000000 1.240000 ( 1.246622) | |
| Symbol.to_proc 1.550000 0.000000 1.550000 ( 1.553462) | |
| ----------------------------------------- total: 2.790000sec | |
| user system total real | |
| Method invoke 1.260000 0.000000 1.260000 ( 1.262633) | |
| Symbol.to_proc 1.420000 0.000000 1.420000 ( 1.424335) |
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
| require 'crawlers/yellow_pages_crawler' | |
| class YellowPages | |
| DEFAULT_PAGE_LENGTH = 100 | |
| DEFAULT_CONCURRENT_THREADS = 10 | |
| def initialize(api_key, sandbox_enabled) | |
| @client = YellowApi.new(:apikey => api_key, :sandbox_enabled => sandbox_enabled) | |
| 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
| class YellowPagesCrawler < Pioneer::Base | |
| attr_reader :locations | |
| def initialize(opts = {}) | |
| @locations_business = opts.fetch(:locations, {}) | |
| @locations = @locations_business.keys | |
| super | |
| end | |
| def processing(req) |
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
| public static class DispatcherHelper | |
| { | |
| public static void DelayInvoke(this Dispatcher dispatcher, TimeSpan ts, Action action) | |
| { | |
| DispatcherTimer delayTimer = new DispatcherTimer(); | |
| delayTimer.Interval = ts; | |
| delayTimer.Tick += (s, e) => | |
| { | |
| delayTimer.Stop(); | |
| action(); |
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
| def some_function(a, b, &block) | |
| v = [] | |
| (a .. b).each do |c| | |
| v << block.call(a, c) | |
| end | |
| v | |
| end | |
| some_function(0, 10) do |a, c| | |
| [c, a] |
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
| # BFS | |
| def bfs(source, target) | |
| q = [] | |
| visited = {} | |
| q << source | |
| while !q.empty? | |
| current = q.shift | |
| visited[current] = true |