This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
file = "Gemfile" | |
# ?e:ファイルが存在する | |
test( ?e, file ) # => true | |
# ?z:ファイルサイズが 0 である | |
test( ?z, file ) # => false | |
# ?s:ファイルサイズが 0 でない (ファイルサイズを返す、0 ならば nil) -> Integer|nil | |
test( ?s, file ) # => 63 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "bundler/setup" | |
require "sicuro" | |
TIMELIMIT = 1 | |
MEMLIMIT = 1 | |
Sicuro.setup(TIMELIMIT, MEMLIMIT) | |
common = <<CODE | |
def assert_equal(x, y, message = nil) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] ) * "" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
NewerOlder