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
| $git log --grep "help me" | |
| (END) | |
| $git log --grep "save me" | |
| (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
| @routes ||= ActionDispatch::Routing::RouteSet.new.tap do |r| | |
| r.draw do | |
| resources :games, :only => [:index, :create, :show, :update] | |
| end | |
| end | |
| @routes.recognize_path("/games", {:method => 'GET'}) # => {:action => "index", :controller => "games"} |
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 ChessApp | |
| def self.routes | |
| @routes ||= ActionDispatch::Routing::RouteSet.new.tap do |r| | |
| r.draw do | |
| resources :games, :only => [:index, :create, :show, :update] | |
| end | |
| end | |
| 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
| require 'active_support/core_ext/string/inflections' | |
| class Router | |
| attr_reader :env, :route | |
| def initialize(routes, env) | |
| @env = env | |
| @routes = routes | |
| 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 FoosController < ApplicationController | |
| def index | |
| ### | |
| end | |
| def show | |
| ### | |
| 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
| module FoosController | |
| class Index < ControllerAction | |
| def get | |
| @foos = Foo.all | |
| succeed_with(FooPresenter.new(@foos).to(format)) # if you want more control over presentation, hand off to the presenter | |
| end | |
| end # Index | |
| class Create < ControllerAction | |
| def post |
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
| module GamesController | |
| class Index < ControllerAction | |
| def get | |
| [200, {}, Game.all.to_json] | |
| end | |
| def params | |
| super.except(:deleted_at) # no need for strong_params. specify what goes and what doesn't by overriding the params |
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
| # Assuming your execute command is more complex than a simple puts, | |
| # you will need separate classes | |
| class GoodbyeCommand | |
| def execute | |
| # complex logic here | |
| end | |
| end | |
| class TwitterCommand |
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
| #Given an array of numbers i.e. [1,2,3...1000], find out all possible combinations that add up to any given number i.e. 51437 | |
| #start with empty db | |
| # start with 1st transaction -> write {key = 1, value => 1 } | |
| # for the second transaction write { key => 1.2 => value => 3 } | |
| # { key = 2, value => 3 } | |
| # for the third transaction write { key => 1.2.3 => value => 6 } | |
| # { key = 1.3, value => 4 } | |
| # { key = 2.3, value => 5 } | |
| # { key = 3, value => 3 } |
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
| # Given a table stat with primary key "slug" with data | |
| # [{slug: 'foo', stats: {}}, {slug:'bar', stats:{}}] | |
| updated_stats = { | |
| 'foo' => {a: 1, b: 2}, | |
| 'bar' => {a: 3, b: 4} | |
| } | |
| # I can do this | |
| updated_stats.each{|k,v| |