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
module DartsLive | |
def self.rating(zero_one_stats, cricket_stats) | |
((0.2 * zero_one_stats - 6) + (5.0 * cricket_stats - 4.5)) / 2 | |
end | |
module NumericExtension | |
def need_stats | |
{:zero_one => (self + 6) / 0.2, :cricket => (self + 4.5) / 5} | |
end | |
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 Foo | |
def class_object | |
self.class.to_s.split(/::/).inject(Object) do |c, name| | |
c.const_get(name) | |
end | |
end | |
end | |
Foo.new.class_object #=> Foo |
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
module Enumerable | |
def quicksort! | |
return self if self.size <= 1 | |
piv = self[0] | |
top, bottom = self[1..-1].partition{|i| i < piv} | |
top.quicksort + [piv] + bottom.quicksort | |
end | |
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
"A".upto "Z" do |c| | |
puts c | |
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
module Enumerable | |
def bubblesort! | |
return self if self.size <= 1 | |
(self.size - 1).downto(0) do |i| | |
0.upto(i - 1) do |j| | |
self[j+1], self[j] = self[j], self[j+1] if self[j] > self[j+1] | |
end | |
end | |
self |
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
BEGIN { | |
$a = 10 | |
puts "begin1 #{$a}" | |
} | |
BEGIN { | |
$a = 20 | |
puts "begin2 #{$a}" | |
} |
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
#!/usr/local/env ruby | |
# -*- coding: UTF-8 -*- | |
# -*- coding: EUC-JP -*- #=> このmagic commentは無効 | |
p "test".encoding #=> #<Encoding:UT-8> | |
# Magic Commentは1行目、もしくはshebangが1行目の場合は2行目の場合有効。 | |
# Magic Commentが無い場合、US-ASCIIと判断される。 | |
# Magic Commentの形式 | |
# # coding: UTF-8 |
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
# coding: UTF-8 | |
str_utf = "hoge" | |
str_euc = "hoge".encode("EUC-JP") | |
# ASCII互換コーディングをもつ7bitクリーンな文字列は | |
# エンコーディングに関わらずASCIIとして扱える | |
p str_utf == str_euc #=> true | |
p /hoge/ =~ str_utf #=> 0 |
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
# DuckTypingの単純な例 | |
# ここで重要なのはSTDOUT(IOクラス)と | |
# StringIOクラスには継承関係がないとうこと。 | |
# | |
require 'stringio' | |
def message_write(out, message) | |
out.write(message) | |
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
# coding: UTF-8 | |
class Duck; end | |
def foo(obj) | |
raise TypeError, "アヒルじゃないです" unless obj.is_a? Duck | |
end | |
foo(Object.new) #=> TypeError |
OlderNewer