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' | |
| result = [] | |
| Benchmark.bm(7) do |reports| | |
| reports.report do | |
| (2..500).each do |x| | |
| (2..500).each do |y| | |
| next unless x > y | |
| a = [x*x-y*y, 2*x*y, x*x+y*y] | |
| next unless (a[0] + a[1] + a[2] === 1000) | |
| next unless (a[0]*a[0]+a[1]*a[1] === a[2]*a[2]) |
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
| # This is a modification of the blogpost on how to use Mongoid and Sinatra. | |
| # Here is the original blogpost http://www.garrensmith.com/2010/09/11/Mongoid-sinatra.html | |
| # The modification lets you use Mongoid 3.0 with the new Moped driver | |
| # The new Moped driver uses the Sessions component to define the MongoDB connection rather | |
| # than the Mongo::Connection.new that is used in the case of the default Ruby driver. | |
| # Moped::Session.new vs Mongo::Connection.new | |
| # Mongoid.load!(yaml_config.yml) is used to build the config hash. Alternatively, the hash can | |
| # be built manually by using Mongoid.config {|config| ...} syntax. The config.sessions hash |
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 Email | |
| include Mongoid::Document | |
| field :emailid, type: String | |
| embeds_many :account | |
| end | |
| class Account | |
| include Mongoid::Document | |
| field :number, type: Integer | |
| field :openedon, type: DateTime |
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
| TL;DR | |
| Putting it all together: Make sure you are using the Ruby version that you need to export from | |
| $ gem list > ~/gemlist && rbenv local rbx-1.2.4 && \ | |
| gem install `awk -F' ' '{print $1}' ~/gemlist` && rm ~/gemlist | |
| How I got there: | |
| By exporting to a file: |
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
| # Enumerable module provides most of the list traversal classes, some of which are mixed-in to File class. | |
| # #each_line, #each_slice etc are such methods. | |
| # What if you need to split against a custom symbol. This is particularly useful while processing reports or logs. | |
| # For instance, one of the reports I get is see is of this format: | |
| # \e!C\r | |
| # <some header data> | |
| # ----------------------------------------------------------------------- | |
| # Name Account Address DoB |
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 'faraday' | |
| require 'net/http' | |
| require 'pp' | |
| # Repos: | |
| # https://github.com/technoweenie/faraday | |
| # https://github.com/pengwynn/faraday_middleware | |
| # Blog posts: | |
| # http://adventuresincoding.com/2010/09/writing-modular-http-client-code-with-faraday |
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
| # this simple plumbing makes sure that the return data is in the form of a hash. | |
| require 'faraday' | |
| class Main | |
| attr_reader :conn | |
| def initialize | |
| @conn = Faraday.new "http://127.0.0.1:5984/somedb" do |faraday| | |
| faraday.adapter :em_http | |
| faraday.builder.insert(0, JSONMiddleware) |
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
| If the error has "emfile" in the reason, it most probably has to do with the ulimit. | |
| If the error is a TCP driver error saying that max file descriptors can be only a certain value, it has to do with the compilation of Erlang. This link: http://boorad.github.com/2009/07/09/mac-kpoll.html has info. | |
| Setting the FD_SETSIZE to 8192 worked and limited the TCP driver connections to 8192. | |
| Setting it to 81920 makes CouchDB fail to start. | |
| ^ | |
| | That worked. The reason why couch didnt start up was the setting of mochiweb acceptor pool to an atrocious 81000! | |
| things to look out for: |
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
| # Soundcloud, after authorization, redirects to a pre-registered URL | |
| # with the user's code, access_token embedded in the URL which may | |
| # be an issue! | |
| # Alternatively, modify the #authorize_url() method such that the url | |
| # that gets generated has a parameter "response_type" of "code" instead | |
| # of "code_and_token" which is the default. | |
| require 'sinatra' | |
| require 'soundcloud' |
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
| # ~/.rake is your global rake directory. Rake knows the tasks you write there. | |
| # Create a rake file, say, sinatra.rake | |
| desc "Create a directory structure for Sinatra classic app" | |
| task :create_sinatra do | |
| sh "mkdir public" | |
| sh "touch app.rb" | |
| sh "mkdir public/js" | |
| sh "mkdir public/css" | |
| sh "mkdir public/images" |