View rspec_with_anonymous_controller.rb
1 2 3 4 5 6 7 8 9
require 'rails_helper'
 
RSpec.describe V1::BaseController, :type => :controller do
 
before do
Rails.application.routes.draw do
match "/v1/dummy", to: "v1/base#index", via: :all
end
end
View standup
1 2 3 4 5 6 7 8 9
#!/usr/bin/env ruby
 
require 'date'
 
def open_video(file)
file = file.gsub(/\s/, '\\ ')
puts "opening #{file}"
`open #{file} -n /Applications/MPlayerX.app`
end
View zsh-bira-theme.diff
1 2 3 4 5 6 7 8 9 10
diff --git a/themes/bira.zsh-theme b/themes/bira.zsh-theme
index 2157204..003cb92 100644
--- a/themes/bira.zsh-theme
+++ b/themes/bira.zsh-theme
@@ -4,7 +4,7 @@ local return_code="%(?..%{$fg[red]%}%? ↵%{$reset_color%})"
local user_host='%{$terminfo[bold]$fg[green]%}%n@%m%{$reset_color%}'
local current_dir='%{$terminfo[bold]$fg[blue]%} %~%{$reset_color%}'
local rvm_ruby=''
-if which rvm-prompt &> /dev/null; then
+if [ -e ~/.rvm/bin/rvm-prompt ]; then
View constant_vs_class_variable.rb
1 2 3 4 5 6 7 8 9 10
require 'benchmark'
 
LOCALE_ARRAY = (1..1000000).to_a
 
class MyClass
def self.vars
@@vars
end
 
def self.vars=(args)
View csrf_token_in_rails_ajax_request.markdown
class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
end
$(document).ajaxSend(function(e, xhr, options) {
  var token = $("meta[name='csrf-token']").attr("content");
  xhr.setRequestHeader("X-CSRF-Token", token);
View extend_built_in_objects.coffee
1 2 3 4 5 6 7 8 9
# Use :: to assign your new function to the prototype of the object or class.
# Maintainable JavaScript: Don’t modify objects you don’t own; Extending built-in native objects. Evil or not?
# http://www.slideshare.net/nzakas/maintainable-javascript-1071179
 
unless String::trim then String::trim = -> @replace /^\s+|\s+$/g, ""
 
String::strip = -> if String::trim? then @trim() else @replace /^\s+|\s+$/g, ""
String::lstrip = -> @replace /^\s+/g, ""
String::rstrip = -> @replace /\s+$/g, ""
View sorting-algorithms.rb
1 2 3 4 5 6 7 8 9 10
class Array
# 插入排序
def insert_sort!
(0...self.length).to_a.each do |j|
key = self[j]
i = j - 1;
while i >= 0 and self[i] > key
self[i+1] = self[i]
i = i-1
end
View dynamic_methods1.coffee
1 2 3 4 5 6 7 8 9 10
class MyClass
constructor: (@name) ->
for k, v of ['get', 'set']
console.log('creating method: ' + v)
MyClass::[v] = (args...) ->
method = v
console.log('executing method: ' + method)
o = new MyClass('dummy')
View mixin1.coffee
1 2 3 4 5 6 7 8 9 10
mixOf = (base, mixins...) ->
class Mixed extends base
for mixin in mixins by -1 # earlier mixins override later ones
for name, method of mixin::
Mixed::[name] = method
Mixed
class MixinClassOne
sayOne: -> "Hello One!"
View module_and_class.coffee
1 2 3 4 5 6 7 8 9 10
class @MyModule
@init: ->
alert "Init MyModule"
return
MyModule.init()
 
 
class MyClass
constructor: (name) ->
Something went wrong with that request. Please try again.