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
package main | |
import ( | |
"code.google.com/p/go-tour/wc" | |
"strings" | |
) | |
func WordCount(s string) map[string]int { | |
tokens := strings.Fields(s) | |
hashmap := make(map[string] int) |
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
package main | |
import "fmt" | |
// Fibonacci closure factory | |
func fibonacci() func() int { | |
previous := 0 | |
current := 1 | |
return func() int { |
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
package main | |
import ( | |
"fmt" | |
"math/cmplx" | |
) | |
func Cbrt(x complex128) complex128 { | |
result := complex128(x) | |
for i := 0; i < 100; i++ { // To ensure high precision |
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
package main | |
import ( | |
"fmt" | |
"log" | |
"net/http" | |
) | |
type Hello struct{} |
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 Checklist < ActiveRecord::Base | |
scope :current, -> { | |
c = Arel::Table.new(:checklists, as: 'c') | |
r = Arel::Table.new(:checklists, as: 'r') | |
sql = c.project(Arel.star) | |
.join(r.project(r[:version_id], r[:version_no].maximum.as('vno')) | |
.group(r[:version_id]).as('r')) | |
.on(r[:version_id].eq(c[:version_id]) | |
.and(r[:vno].eq(c[:version_no]))) | |
find_by_sql(sql) |
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 User < PersistentDataStore::Base | |
attr_accessor :first_name, :last_name | |
end | |
class UserView < SimpleDelegator | |
def fullname | |
"#{first_name} #{last_name}" | |
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
class UserView < SimpleDelegator | |
def initialize(delegate, salutation: 'Padawan') | |
super(delegate) | |
@salutation = salutation | |
end | |
def fullname | |
"#{salutation} #{first_name} #{last_name}" | |
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 ViewHelpers | |
def urls | |
Rails.application.routes.url_helpers | |
end | |
def helpers | |
ActionController::Base.helpers | |
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
module ViewHelpers | |
extend ActiveSupport::Concern | |
included do | |
def default_url_options | |
ActionMailer::Base.default_url_options | |
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
class DashboardView | |
def initialize(current_user, users, featured_posts) | |
@current_user = current_user | |
@users = users.map { |u| UserView.new(u) } | |
@featured_posts = featured_posts | |
end | |
attr_reader :current_user, :users, :featured_posts | |
end | |
# Instantiate this in your controller layer and pass it to the view as the only "source of truth" for the data on the page |
OlderNewer