Skip to content

Instantly share code, notes, and snippets.

Avatar

Junichi Ito JunichiIto

View GitHub Profile
View qiita.js
(function(){"use strict";var a,b,c,d,e,f,g,h,i,j;a=function(){function a(a){var b,d,e,f;this.title=a.title,this.url=a.url,this.tags=[],f=a.tags;for(d=0,e=f.length;d<e;d++)b=f[d],this.tags.push({name:b.name,url:"http://qiita.com/tags/"+b.name});this.user=new c(a.user)}return a}(),b=function(){function b(){}return b.prototype.itemContainer={},b.prototype.findByUsername=function(b,c){var d=this;if(b in this.itemContainer){c(this.itemContainer[b]);return}return e("https://qiita.com/api/v1/users/"+b+"/items?per_page=10",function(e){var f,g,h;d.itemContainer[b]=[];for(g=0,h=e.length;g<h;g++)f=e[g],d.itemContainer[b].push(new a(f));return c(d.itemContainer[b])})},b}(),c=function(){function a(a){this.name=a.url_name,this.profileImageUrl=a.profile_image_url,this.url="http://qiita.com/users/"+this.name}return a}(),e=function(a,b){var c,d,e=this;c="get",d=new XMLHttpRequest;if("withCredentials"in d)d.open(c,a,!0);else{if(typeof XDomainRequest=="undefined")throw"Failed to initialize CORSRequest";d=new XDomainRequest,d.op
@JunichiIto
JunichiIto / gist:9870182
Created Mar 30, 2014
Ruby: How to count for each word
View gist:9870182
str = "no ruby no life" #=> count as {"no"=>2,"ruby"=>1,"life"=>1}
Hash[str.scan(/\w+/).group_by{|s|s}.map{|k,v|[k,v.size]}]
str.scan(/\w+/).each_with_object(Hash.new(0)){|s,h|h[s]+=1}
# require Ruby 2.1.1
str.scan(/\w+/).group_by{|s|s}.map{|k,v|[k,v.size]}.to_h
@JunichiIto
JunichiIto / gist:10015280
Created Apr 7, 2014
Sample code to find next/prev user by date_of_birth.
View gist:10015280
user = User.find params[:id]
older_user = User.where("date_of_birth <= ? AND id <> ?", user.date_of_birth, user.id).order("date_of_birth DESC, id").limit(1).first
younger_user = User.where("date_of_birth >= ? AND id <> ?", user.date_of_birth, user.id).order("date_of_birth ASC, id").limit(1).first
View gist:a87fe61c4f28e3268804
(define (total_count_for amount)
(+ amount (/ (abs (pred amount)) 2)))
View gist:e27150de2700e92983bb
class Event < ActiveRecord::Base
has_many :groups
# reference: http://guides.rubyonrails.org/association_basics.html#self-joins
has_many :next_events, class_name: "Event", foreign_key: "previous_event_id"
belongs_to :previous_event, class_name: "Event"
end
# schema.rb
create_table "events", force: :cascade do |t|
View gist:891d1a8eb1f5efded351
# Gemfile
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.0'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
@JunichiIto
JunichiIto / bingo_spec.rb
Created Mar 5, 2015
ビンゴカード作成問題・解答テンプレート (https://codeiq.jp/ace/ito_junichi/q1326)
View bingo_spec.rb
# coding: utf-8
class Bingo
def self.generate_card
# 以下の行は削除して、自分でロジックを実装してください。
sample
end
def self.sample
<<-CARD
View sum_matrix_test.rb
require 'minitest/autorun'
module SumMatrix
extend self
def generate_sum_matrix(col: 4, row: 4, number_range: 1..1000)
matrix = generate_matrix(col: col, row: row, number_range: number_range)
format_matrix(sum_matrix(matrix))
end
View fill_blank_test.rb
require 'test/unit'
require 'csv'
class FillBlankTest < Test::Unit::TestCase
def fill_blank(csv_text)
CSV.parse(csv_text).inject([]) { |results, row|
results << row.zip(results.last || []).map { |curr, prev| curr || prev }
}
.map { |row| row.join(',') }
.join("\n")
View true_truthy_spec.rb
describe 'Test true/truthy' do
context 'test 1' do
example do
expect(1).to be_truthy
end
example do
expect(1).to be true
end
example do
expect(1).to eq true