Skip to content

Instantly share code, notes, and snippets.

View shinokada's full-sized avatar

Shinichi Okada shinokada

View GitHub Profile
@shinokada
shinokada / Namespace with Object literal
Created January 7, 2013 02:59
Developing backbone.js applications page 29
/*Doesn't check for existence of myApplication*/
var myApplication = {};
/*
Does check for existence. If already defined, we use that instance.
Option 1: if(!myApplication) myApplication = {};
Option 2: var myApplication = myApplication || {};
We can then populate our object literal to support models, views and collections (or any data, really):
*/
var myApplication = {
models : {},
@shinokada
shinokada / nested_object_namespacing_with_the_object_literal_pattern
Created January 7, 2013 03:17
nested object namespacing with the object literal pattern from Developing Backbone.js Applications p31.
var galleryApp = galleryApp || {};
// perform similar check for nested children
galleryApp.routers = galleryApp.routers || {};
galleryApp.model = galleryApp.model || {};
galleryApp.model.special = galleryApp.model.special || {};
// routers
galleryApp.routers.Workspace = Backbone.Router.extend({});
galleryApp.routers.PhotoSearch = Backbone.Router.extend({});
// models
source 'https://rubygems.org'
ruby '2.0.0'
#ruby-gemset=railstutorial_rails_4_0
gem 'rails', '4.0.0'
group :development do
gem 'sqlite3', '1.3.7'
end
# To see what in the default register
:reg "
# To see what in the register 0
:reg 0 # yank register
@shinokada
shinokada / file0.txt
Created December 13, 2013 09:55
Vim-instatnt-markdown+Mavericks+zsh ref: http://qiita.com/sokada/items/0bfac2e6717f21504ee8
gem install pygements.rb
gem install redcarpet
npm -g install instant-markdown-d
@shinokada
shinokada / amidakuji.rb
Last active August 29, 2015 14:00
Swap two items in an array
a = [1, 2, 3, 4]
(0...arr.length - 1).map{|i| arr.dup.tap{ |a| a[i, 2] = a[i, 2].reverse } }
# => [[2, 1, 3, 4, 5], [1, 3, 2, 4, 5], [1, 2, 4, 3, 5], [1, 2, 3, 5, 4]]
a = [1, 2, 3]
# => [1, 2, 3]
(0...a.length - 1).map{|i| a.dup.tap{|a| a[i, 2] = a[i, 2].reverse}}
# => [[2, 1, 3], [1, 3, 2]]
# read text from file, map to integer and sum all
File.new(file_name, 'r').each_line.map(&:strip).map(&:to_i).inject(:+)
require 'matrix'
arr = [
%w(J O I J O),
%w(I J O J O),
%w(I I J I J)
]
myarr1 = Array(arr)
# [["J", "O", "I", "J", "O"], ["I", "J", "O", "J", "O"], ["I", "I", "J", "I", "J"]]
# http://stackoverflow.com/questions/23382474/is-there-any-way-to-create-2-x-2-array-matrix-from-a-larger-array-matrix
require 'matrix'
def find_in_matrix(arr,sub)
sub_nrows = sub.size
sub_ncols = sub.first.size
rows = Array(0..(arr.size - sub_nrows))
cols = Array(0..(arr.first.size - sub_ncols))
arr_m = Matrix[*arr]
sub_m = Matrix[*sub]
[[0, 0, 3], [0, 1, 4], [1, 0, 5], [1, 1, 6]].select{ |i, j, k| p "#{ i } is #{ j } is #{ k }" }
# "0 is 0 is 3"
# "0 is 1 is 4"
# "1 is 0 is 5"
# "1 is 1 is 6"
# => [[0, 0, 3], [0, 1, 4], [1, 0, 5], [1, 1, 6]]