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
# 定数参照 | |
### ・クラス/モジュールで定義された定数は、その定数が定義されたクラス/モジュールに格納される | |
### ・クラス/モジュール定義の外(トップレベル)で定義された定数は Object に格納される | |
### ・メソッドの中では定義できません | |
## ネスト | |
### ・定数はその定数が定義されたクラス/モジュール定義の中(メソッドやネストしたクラス/モジュールも含む)から参照できる | |
CONST = "TOP" | |
module A | |
CONST = "A" | |
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
# String#bytes.to_aよりもString#unpack("C*") |
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 FizzBuzz | |
def initialize( from = 1, to = 30 ) | |
@from, @to = [ from, to ].minmax | |
block_given? ? yield( self ) : self | |
end | |
def spec( out, &cond ) | |
( @specs ||= [] ) << lambda { |e| cond[ e ] ? out : nil } | |
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
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 } |
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
# 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
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
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 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
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 |
OlderNewer