Skip to content

Instantly share code, notes, and snippets.

View shuhei's full-sized avatar
🐶
This is fine

Shuhei Kagawa shuhei

🐶
This is fine
View GitHub Profile
@shuhei
shuhei / b64.rb
Created August 3, 2013 06:44
Base64 encoding
module B64
extend self
ENCODING_TABLE = [
('A'..'Z').to_a,
('a'..'z').to_a,
('0'..'9').to_a,
['+', '/']
].flatten
@shuhei
shuhei / object_keys.js
Last active December 20, 2015 23:59
In JavaScript, object keys are always strings.
var obj = {};
// null and 'null'
obj[null] = 1;
obj['null'] = 2;
console.log(obj[null]); // 2
// Numbers
obj[3] = 3;
obj['3'] = 4;
@shuhei
shuhei / should_to_expect.rb
Last active December 21, 2015 08:09
RSpec's should style to expect style
obj.should ...
expect(obj).to ...
obj.should_not ...
expect(obj).not_to ...
obj.should =~ //
expect(obj).to match(//)
[1, 2, 3].should =~ [3, 2, 1]
@shuhei
shuhei / config_initializers_konacha.rb
Last active December 21, 2015 13:58
Prevent paper_trails from causing errors in konacha server. Konacha::SpecsController doesn't need paper_trails anyway because it only serves spec files.
Konacha::SpecsController.class_eval do
def paper_trail_enabled_for_controller
false
end
end if defined?(Konacha)
@shuhei
shuhei / prettyJson.js
Created September 1, 2013 06:16
Print prettified JSON from file or STDIN. Save this file as `~/bin/json`.
#!/usr/bin/env node
var filename = process.argv[2];
if (filename) {
var json = require('fs').readFileSync(filename).toString('utf-8');
printPretty(json);
} else {
var json = '';
process.stdin.setEncoding('utf-8');
process.stdin.on('data', function (chunk) {
@shuhei
shuhei / date_time_comparison.rb
Created September 8, 2013 04:43
Active Support's inconsistency in Time-Date comparison.
# My local time offset is +0900.
require 'date'
require 'time'
Time.new(2013, 9, 3, 10) > Date.new(2013, 9, 3) # ArgumentError: comparison of Time with Date failed
require 'active_support/time'
@shuhei
shuhei / elem_count.rb
Created October 16, 2013 06:03
Assert number of elements with Capybara. Let's say we are testing a page whose JS adds elements step by step...
# Bad
expect(all('.something').size).to eq(5)
# Good
expect(page).to have_css('.something', count: 5)
@shuhei
shuhei / wp2op.rb
Created October 26, 2013 14:59
WordPress to Octopress
require 'nokogiri'
xml = File.read('./wordpress.2013-07-10.xml')
doc = Nokogiri::XML.parse(xml)
items = doc.xpath('/rss/channel/item')
puts "#{items.size} items"
items.each do |item|
date = item.at('./wp:post_date').text
@shuhei
shuhei / RootController.scala
Created November 9, 2013 15:00
Akka with skinny-framework. Combined skinny-framework's boilerplate and Scalatra's akka example. http://scalatra.org/2.2/guides/async/akka.html
package controller
import akka.actor.ActorSystem
import scala.concurrent.{Promise, ExecutionContext}
import org.scalatra.{AsyncResult, FutureSupport}
import dispatch._
import skinny.controller.SkinnyServlet
object DispatchAkka {
def retrievePage()(implicit cxt: ExecutionContext): Future[String] = {
@shuhei
shuhei / binary_tree.go
Created November 11, 2013 05:32
(A Tour of Go #70) Exercise: Equivalent Binary Trees http://tour.golang.org/#70
package main
import (
"code.google.com/p/go-tour/tree"
"fmt"
)
// Walk walks the tree t sending all values
// from the tree to the channel ch.
func Walk(t *tree.Tree, ch chan int) {