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 'async' | |
| require 'async/http/endpoint' | |
| require 'async/websocket/adapters/rack' | |
| require 'falcon' | |
| websocket_endpoint = Async::HTTP::Endpoint.parse('http://127.0.0.1:3000') | |
| module WebSocketApp | |
| def self.call(env) |
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 'benchmark' | |
| Benchmark.bm do |bm| | |
| repeat = 10000000 | |
| bm.report('send') { repeat.times { 5.send(:>, 2) } } | |
| bm.report('method.call') { repeat.times { 5.method(:>).call(2) } } | |
| end | |
| # user system total real |
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 String | |
| def substrings | |
| ((0...length).to_a.combination(2).map do |from,to| | |
| # Extract substrings | |
| self[from, to] | |
| end + self.chars).map do |s| | |
| # Trim off any unwanted characters | |
| s.sub(/\A\W+/, '').sub(/\W+\z/, '') | |
| end.sort.uniq.reject do |s| | |
| # Remove empty strings |
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 'nokogiri' | |
| doc = Nokogiri::HTML(DATA) | |
| doc.css('head').remove | |
| puts doc.to_s | |
| __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
| #!/usr/bin/env ruby | |
| require 'benchmark' | |
| COUNT = 1000 | |
| LOOP = 1000 | |
| def case_method_rb | |
| [ | |
| "def case_method(x)", |
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
| extern crate mio; | |
| use std::error::Error; | |
| use mio::*; | |
| use mio::tcp::{TcpListener, TcpStream}; | |
| // Setup some tokens to allow us to identify which event is | |
| // for which socket. | |
| const SERVER: Token = Token(0); |
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 are_okay?(list) | |
| list.all? do |i| | |
| begin | |
| # Arbitrary method call | |
| i.floor | |
| true | |
| rescue | |
| # Should rescue from known exception (or Pokemon capture) |
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
| namespace :db do | |
| namespace :sequences do | |
| desc 'Brings Postgres database sequences into sync with MAX(id)' | |
| task sync: :environment do | |
| ActiveRecord::Base.connection.execute("SET search_path TO public") | |
| updates = ActiveRecord::Base.connection.select_rows(%Q{ | |
| SELECT 'SELECT setval('''||_schema||'.'||_seq||''', (SELECT MAX('||_column||') FROM '||_schema||'.'||_table||')+1); ' AS updatestring | |
| FROM ( | |
| SELECT n.nspname AS _schema, |
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
| var refresh_element = $('#refresh')[0]; | |
| if (refresh_element) { | |
| $(refresh_element).data('refresh', 1); | |
| setInterval(function() { | |
| if ($(refresh_element).data('refresh')) | |
| $(refresh_element).load(window.location.href + ' #refresh'); | |
| }, 5000); |
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
| #!/usr/bin/env ruby | |
| require 'benchmark' | |
| def with_call(object, &block) | |
| block.call(object) | |
| end | |
| def with_proc(object) | |
| Proc.new.call(object) |
NewerOlder