Ruby 1.8.7 リファレンスマニュアル > ライブラリ一覧 > 組み込みライブラリ > Rangeクラス

class Range

クラスの継承リスト: Range < Enumerable < Object < Kernel

要約

範囲オブジェクトのクラス。範囲オブジェクトは範囲演算子 .. または ... によって生成されます。.. 演算子によって生成された範囲 オブジェクトは終端を含み、... 演算子によって生成された範囲オブジェ クトは終端を含みません。

for i in 1..5
   # 処理
end

これは 1 から 5 までの範囲オブジェクトを生成して、それぞれの値に対して 繰り返すと言う意味です。

範囲演算子のオペランドは互いに <=> で比較できる必要があります。 さらに Range#each を実行するためには succ メソッ ドを実行できるものでなければいけません。

破壊的な変更

Ruby の Range クラスは immutable です。 つまり、オブジェクト自体を破壊的に変更することはできません。 ですので、一度生成された Range のオブジェクトの指し示す範囲は 決して変更することはできません。

range = 1..10
range.first     #=> 1
range.first = 1 #=> NoMethodError

特異メソッド

定義 説明
new(first, last, exclude_end = false) -> Range

first から last までの範囲オブジェクトを生成して返しま す。

インスタンスメソッド

定義 説明
self == other -> bool

指定された other が Range クラスのインスタンスであり、 始点と終点が == メソッドで比較して等しく、Range#exclude_end? が同じ場合に true を返します。そうでない場合に false を返します。

self === obj -> bool
include?(obj) -> bool
member?(obj) -> bool

obj が範囲内に含まれている時に真を返します。

begin -> object
first -> object

最初の要素を返します。

each {|item| ... } -> self
each -> Enumerable::Enumerator

範囲内の要素に対して繰り返します。

end -> object
last -> object

終端を返します。範囲オブジェクトが終端を含むかどうかは関係ありませ ん。

eql?(other) -> bool

指定された other が Range クラスのインスタンスであり、 始点と終点が eql? メソッドで比較して等しく、Range#exclude_end? が同じ場合に true を返します。そうでない場合に false を返します。

equal?(other) -> bool

指定された other が self 自身である場合のみ真を返します。 これは Object クラスで定義されたデフォルトの動作で す。

exclude_end? -> bool

範囲オブジェクトが終端を含まないとき真を返します。

hash -> Integer

始点と終点のハッシュ値と Range#exclude_end? の値からハッシュ値を計算して整数として返します。

step(s = 1) {|item| ... } -> self
step(s = 1) -> Enumerable::Enumerator

範囲内の要素を s おきに繰り返します。

継承したメソッド

all? any? collect count cycle detect drop drop_while each_cons each_slice each_with_index entries find_all find_index grep group_by inject max max_by min min_by minmax minmax_by none? one? partition reject reverse_each sort sort_by take take_while to_set zip =~ __id__ __send__ _dump class clone dclone display enum_for extend freeze frozen? initialize initialize_copy inspect instance_eval instance_exec instance_of? instance_variable_defined? instance_variable_get instance_variable_set instance_variables is_a? marshal_dump marshal_load method method_missing methods nil? pretty_inspect pretty_print pretty_print_cycle pretty_print_inspect pretty_print_instance_variables private_methods protected_methods public_methods remove_instance_variable respond_to? singleton_method_added singleton_method_removed singleton_method_undefined singleton_methods taint tainted? tap to_ary to_hash to_int to_io to_proc to_regexp to_s to_str to_yaml to_yaml_properties to_yaml_style untaint