Skip to content

Instantly share code, notes, and snippets.

View JunichiIto's full-sized avatar

Junichi Ito JunichiIto

View GitHub Profile
@JunichiIto
JunichiIto / gist:9870182
Created March 30, 2014 09:29
Ruby: How to count for each word
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 / count_by_word_spec.rb
Last active October 2, 2020 12:11
Sample implementation for count_by_word. Please see: http://qiita.com/jnchito/items/c4a56046be1096c19b1c
def count_by_word(string)
string
.scan(/\w+/)
.group_by{|s| s}
.map{|word, words| [word, words.size]}
.to_h # require Ruby 2.1 or higher
end
describe "count by word" do
it "counts by word" do
def count_by_word(string)
string
.scan(/\w+/)
.each_with_object(Hash.new(0)){|s, h| h[s] += 1 }
end
def count_by_word(string)
fn = ->(s, h){ h[s] += 1 }
string
.scan(/\w+/)
.each_with_object(Hash.new(0), &fn)
end
@JunichiIto
JunichiIto / gist:10015280
Created April 7, 2014 05:34
Sample code to find next/prev user by date_of_birth.
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
(define (total_count_for amount)
(+ amount (/ (abs (pred amount)) 2)))
@JunichiIto
JunichiIto / karaoke_machine.rb
Created June 27, 2014 09:51
CodeIQに出題した「カラオケマシン問題」の解答テンプレートです。詳しくはこちら: http://blog.jnito.com/entry/2014/06/06/104420
# coding: utf-8
class KaraokeMachine
def initialize(melody)
end
def transpose(amount)
"" # これは仮実装なので消してください
end
end
@JunichiIto
JunichiIto / alias_matchers.md
Last active April 16, 2024 16:18
List of alias matchers in RSpec 3

This list is based on aliases_spec.rb.

You can see also Module: RSpec::Matchers API.

matcher aliased to description
a_truthy_value be_truthy a truthy value
a_falsey_value be_falsey a falsey value
be_falsy be_falsey be falsy
a_falsy_value be_falsey a falsy value
@JunichiIto
JunichiIto / gist:e27150de2700e92983bb
Last active August 29, 2015 14:15
Sample for self joins
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|
@JunichiIto
JunichiIto / gist:891d1a8eb1f5efded351
Created February 20, 2015 00:15
Skip JSON format in rails generate scaffold => http://stackoverflow.com/a/24104811/1058763
# 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'