Skip to content

Instantly share code, notes, and snippets.

View eiel's full-sized avatar

HIMURA Tomohiko eiel

View GitHub Profile
@eiel
eiel / active_support_time_sample.rb
Created February 7, 2013 04:12
ブログで使うサンプルコード
require 'active_support/all'
datetime = DateTime.new 2013, 2, 7
date = Date.new 2013, 2, 7
time = Time.new 2013, 2, 7
datetime # => Thu, 07 Feb 2013 00:00:00 +0000
date # => Thu, 07 Feb 2013
time # => 2013-02-07 00:00:00 +0900
@eiel
eiel / player_02.5.rb
Last active December 12, 2015 08:59 — forked from Shinpeim/player_02.rb
https://gist.github.com/Shinpeim/4745446 へ変化するコードなのだけど、リファクタリング的にみると飛躍があってその中間をかくとどうなるか。 というのを考えてみる。パターン1。 なかなか興味深くて、こういうプログラムをかける人はレアな気がする。 故にStateパターンと名前がついてる方向があるので迷わず済むのかなとか思った。 蛇足だけど、 @moving_mode には Rubyなら普通は シンボルを突っ込む気がする。
# -*- coding: utf-8 -*-
class Player
attr_reader :position
# Cで言うenum的なやつ
module MOVING_MODE
NORMAL = 1
FAST = 2
KANI = 3 #蟹モードが増えた
end
@eiel
eiel / player_02.5.2.rb
Last active December 12, 2015 08:59 — forked from Shinpeim/player_02.rb
https://gist.github.com/Shinpeim/4745446 へ変化するコードなのだけど、リファクタリング的にみると飛躍があってその中間をかくとどうなるか。 というのを考えてみる。パターン2。 いきなりStateパターンへの方向転換を試みる。コピペして修正。という流れになる。 これもStateパターンと名前がついてる方向があるので迷わず済むのかなー。リファクタリングのステップを検討するのはなかなか面白い。
# -*- coding: utf-8 -*-
class Player
class Normal
# move の内容をもってきて、いらない caseを削除
# return を追加
# x_speed, y_speed を展開すれば 03のコードに向う
def move(direction, position)
x_speed = 10
y_speed = 10
@eiel
eiel / gist:5148886
Created March 13, 2013 02:20
CIELCH 色空間 をHTML で確認するスクリプト。 標本数をいじりたいなら hues chromas lightnesses あたりをいじる。 validRGB でぶっとんだ RGB値のものを省いてるけど、はぶかなくても面白い。
import Data.Colour.CIE
import Data.Colour.SRGB
import Data.Colour.CIE.Illuminant
import Data.Colour.SRGB.Linear
cieLCH :: (Ord a, Floating a) => Chromaticity a -> a -> a -> a -> Colour a
cieLCH white_ch l c h = cieLAB white_ch l a b
where
radius = pi * h / 180
a = cos radius * c
@eiel
eiel / convert_to_type.rb
Created March 20, 2013 05:59
ActiveRecordを何気なくつかってると気づかないけど、代入時に型変換してくれる。ActiveModel でフォームつかってたときに bool にするにはどうすればいいか悩んだので調べたら ActiveRecord::ConnectionAdapters::Column.value_to_boolean を使えば良さそう。
# $ gem install active_record sqlite3
require 'active_record'
require 'sqlite3'
DATABASE_FILE = File.join(File.dirname(__FILE__), "convert.sqlite")
`rm #{DATABASE_FILE}`
ActiveRecord::Base.establish_connection(
adapter: "sqlite3",
database: DATABASE_FILE,
@eiel
eiel / maybe.rb
Last active August 1, 2018 00:40
Ruby でモナドしてみる。
# >> を *
# >>= を bind
# return を self.new
# mplus を +
# mzero を self.zero
#
# に見立てて Maybe モナド書いてみた
# bind に渡す block で Maybe と書きたくないので第二引数に型情報を付加してみた。
class Monad
def *(m)
@eiel
eiel / t.rb
Created April 11, 2013 07:40
通知するだけ。 コマンドの最後に ;t とつけて使う。 必須 gem install terminal-notifier
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
message = ARGV.join(" ")
if message.empty?
message = 'なにか終わったかも'end
system "terminal-notifier -message #{message} -execute 'open -a iTerm.app'"
@eiel
eiel / fizzbuzz.rb
Created May 11, 2013 04:43
#oso2013 fizzbuzz をテストファーストでやっていたので。 その場で Ruby でかいといた。
class FizzBuzz
def fizzbuzz(n)
if n % 3 == 0 and n % 5 == 0
'fizzbuzz'
elsif n % 3 == 0
'fizz'
elsif n % 5 == 0
'buzz'
else
n.to_s
@eiel
eiel / ruby-mode-init.el
Last active December 17, 2015 12:39
マジコメを自動挿入したくないので、こういうの入れたのをふと思い出したので抜粋。
(defun ruby-mode-hook-init ()
"encodingを自動挿入しないようにする"
(remove-hook 'before-save-hook 'ruby-mode-set-encoding)
(define-key ruby-mode-map "\C-ce" 'my-ruby-mode-set-encoding))
(add-hook 'ruby-mode-hook 'ruby-mode-hook-init)
(defun my-ruby-mode-set-encoding ()
"set-encoding ruby-mode"
(interactive)
@eiel
eiel / gist:5689554
Last active December 17, 2015 23:29
github flow で遊んでるんだけど、その流れ。