Skip to content

Instantly share code, notes, and snippets.

@masassiez
masassiez / arara_case.rb
Created February 24, 2012 18:53
Rubyのcase式で気を付けること ref: http://qiita.com/items/2796
require './strange_case.rb'
strange_case( "1" ) # => true
strange_case( :"1" ) # => true
strange_case( 1 ) # => true
strange_case( [1, 2, 3] ) # => false
strange_case( {"1"=>1} ) # => true
strange_case( (1..3) ) # => false
strange_case( /.*/ ) # => false
strange_case( Fixnum ) # => false
@masassiez
masassiez / test.rb
Created February 17, 2012 16:22
便利な Kernel#test の紹介です ref: http://qiita.com/items/2607
file = "Gemfile"
# ?e:ファイルが存在する
test( ?e, file ) # => true
# ?z:ファイルサイズが 0 である
test( ?z, file ) # => false
# ?s:ファイルサイズが 0 でない (ファイルサイズを返す、0 ならば nil) -> Integer|nil
test( ?s, file ) # => 63
@masassiez
masassiez / prime_factorization.rb
Created February 16, 2012 20:09
Rubequeローカル実行確認用テンプレート ref: http://qiita.com/items/2561
require "bundler/setup"
require "sicuro"
TIMELIMIT = 1
MEMLIMIT = 1
Sicuro.setup(TIMELIMIT, MEMLIMIT)
common = <<CODE
def assert_equal(x, y, message = nil)
@masassiez
masassiez / prime_factorization.rb
Created February 15, 2012 14:36
#42@Rubeque - Prime Factorization
def prime_factorization(num)
prime = Enumerator.new do |y|
mem = []
is_prime = lambda { |e| mem.find { |_| e.gcd(_) != 1 } }
2.upto(num) do |n|
unless is_prime[n]
mem << n
y << n
end
@masassiez
masassiez / dupli.rb
Created February 15, 2012 14:33
Rubyでselfを返すメソッドってあったっけ? ref: http://qiita.com/items/2524
class Object
def tap
block_given? ? super : self
end
end
class Array
def dupli
self.group_by(&:tap).reject{ |k, v| v.one? }.keys
end
@masassiez
masassiez / fizzbuzz_chain.rb
Created February 6, 2012 03:23
Ruby1.9の勉強に使った FizzBuzz サンプル ref: http://qiita.com/items/2026
puts 1.upto(100).with_object(nil)
.map { |n, o| ( o ||= [] ) << :Fizz if n % 3 == 0; [n, o] }
.map { |n, o| ( o ||= [] ) << :Buzz if n % 5 == 0; [n, o] }
.map { |n, o| ( o || [n] ) * "" }
@masassiez
masassiez / pythagorean.rb
Created January 27, 2012 13:56
n本の様々な長さの棒の中から3本使って、できるだけ周長の長い三角形を作る | ruby pythagorean.rb n(省略時:15)
class Pythagorean
def initialize(n)
@sticks = [ *1 .. ( { 0 => 15 }[n] || n ) ]
@triples = @sticks.map(&:to_f).combination(3)
end
def triples
@triples.select( &theorem ).map { |e| e.map(&:to_i) }
end
@masassiez
masassiez / time.rb
Created January 27, 2012 07:53
Ruby1.8.7で前日のTimeオブジェクトを取得する
# ruby 1.8.7 only
# 前日のTimeオブジェクトを取得する
## パターン1:Timeクラスに Time#prev_day を追加
class Time
def next_day( n = 1, reset = true )
time = self + 60 * 60 * 24 * n
_hour, _min, _sec = reset ? [ 0, 0, 0 ] : [ hour, min, sec ]
Time.local( time.year, time.month, time.day, _hour, _min, _sec )
@masassiez
masassiez / fib.rb
Created January 26, 2012 09:33
pik bench fib.rb - VMwareFusion WindowsXP
cache = { 0 => 0, 1 => 1 }
fib = lambda {_fib_ = lambda {|n| cache[n] ||= _fib_[n-2] + _fib_[n-1] } }.call
(1..100000).each { |e| fib[e] }
__END__
ruby 1.8.7 (2011-12-28 patchlevel 357) [i386-mingw32]
Rehearsal ------------------------------------------------------------
** benchmarking 'fib.rb' 2.156000 0.469000 2.625000 ( 2.734410)
--------------------------------------------------- total: 2.625000sec
@masassiez
masassiez / rescue_fizzbuzz.rb
Created January 26, 2012 02:00
rescue FizzBuzz
def fizzbuzz(limit)
e ||= 0
( e += 1 ) > limit ? return : raise
rescue lambda { |_| e%15==0 }
puts "FizzBuzz"; retry
rescue lambda { |_| e%5==0 }
puts "Buzz"; retry
rescue lambda { |_| e%3==0 }
puts "Fizz"; retry
rescue lambda { |_| e }