U:RDoc::NormalClass[iI" Hash:ET@I"Object;To:RDoc::Markup::Document:@parts[o;;[3o:RDoc::Markup::Paragraph;[ I"MA Hash is a dictionary-like collection of unique keys and their values. ;TI"NAlso called associative arrays, they are similar to Arrays, but where an ;TI"KArray uses integers as its index, a Hash allows you to use any object ;TI"
type.;To:RDoc::Markup::BlankLine o; ;[I"LHashes enumerate their values in the order that the corresponding keys ;TI"were inserted.;T@o; ;[I"=A Hash can be easily created by using its implicit form:;T@o:RDoc::Markup::Verbatim;[I"3grades = { "Jane Doe" => 10, "Jim Doe" => 6 }
;T:@format0o; ;[I"AHashes allow an alternate syntax for keys that are symbols. ;TI"Instead of;T@o;;[I"=options = { :font_size => 10, :font_family => "Arial" }
;T;0o; ;[I"You could write it as:;T@o;;[I"7options = { font_size: 10, font_family: "Arial" }
;T;0o; ;[I"7Each named key is a symbol you can access in hash:;T@o;;[I""options[:font_size] # => 10
;T;0o; ;[I"9A Hash can also be created through its ::new method:;T@o;;[I"grades = Hash.new
;TI"grades["Dorothy Doe"] = 9
;T;0o; ;[I"JHashes have a default value that is returned when accessing ;TI"Mkeys that do not exist in the hash. If no default is set +nil+ is used. ;TI"LYou can set the default value by sending it as an argument to Hash.new:;T@o;;[I"grades = Hash.new(0)
;T;0o; ;[I"&Or by using the #default= method:;T@o;;[I"!grades = {"Timmy Doe" => 8}
;TI"grades.default = 0
;T;0o; ;[I"8Accessing a value in a Hash requires using its key:;T@o;;[I"$puts grades["Jane Doe"] # => 0
;T;0S:RDoc::Markup::Heading:
leveli: textI"Common Uses;T@o; ;[I"AHashes are an easy way to represent data structures, such as;T@o;;[I"books = {}
;TI"5books[:matz] = "The Ruby Programming Language"
;TI"1books[:black] = "The Well-Grounded Rubyist"
;T;0o; ;[ I"HHashes are also commonly used as a way to have named parameters in ;TI"Lfunctions. Note that no brackets are used below. If a hash is the last ;TI"Margument on a method call, no braces are needed, thus creating a really ;TI"clean interface:;T@o;;[I".Person.create(name: "John Doe", age: 27)
;TI"
;TI"def self.create(params)
;TI" @name = params[:name]
;TI" @age = params[:age]
;TI" end
;T;0S;
;i;I"Hash Keys;T@o; ;[I"OTwo objects refer to the same hash key when their hash
value ;TI"Jis identical and the two objects are eql?
to each other.;T@o; ;[I"MA user-defined class may be used as a hash key if the hash
;TI"Hand eql?
methods are overridden to provide meaningful ;TI"Kbehavior. By default, separate instances refer to separate hash keys.;T@o; ;[I"CA typical implementation of hash
is based on the ;TI"Pobject's data while eql?
is usually aliased to the overridden ;TI"==
method:;T@o;;[#I"class Book
;TI"# attr_reader :author, :title
;TI"
;TI"% def initialize(author, title)
;TI" @author = author
;TI" @title = title
;TI" end
;TI"
;TI" def ==(other)
;TI"" self.class === other and
;TI"' other.author == @author and
;TI"! other.title == @title
;TI" end
;TI"
;TI" alias eql? ==
;TI"
;TI" def hash
;TI"* @author.hash ^ @title.hash # XOR
;TI" end
;TI" end
;TI"
;TI"3book1 = Book.new 'matz', 'Ruby in a Nutshell'
;TI"3book2 = Book.new 'matz', 'Ruby in a Nutshell'
;TI"
;TI"reviews = {}
;TI"
;TI")reviews[book1] = 'Great reference!'
;TI"*reviews[book2] = 'Nice and compact!'
;TI"
;TI"reviews.length #=> 1
;T;0o; ;[I")See also Object#hash and Object#eql?;T:
@fileI"hash.c;T:0@omit_headings_from_table_of_contents_below0;0;0[ [ [[I"Enumerable;To;;[ ;@“;0I"hash.c;T[[I"
class;T[[:public[[I"[];T@›[I"new;T@›[I"try_convert;T@›[:protected[ [:private[ [I"
instance;T[[;[F[I"<;T@›[I"<=;T@›[I"==;T@›[I">;T@›[I">=;T@›[I"[];T@›[I"[]=;T@›[I" any?;T@›[I"
assoc;T@›[I"
clear;T@›[I"compact;T@›[I"
compact!;T@›[I"compare_by_identity;T@›[I"compare_by_identity?;T@›[I"default;T@›[I"
default=;T@›[I"default_proc;T@›[I"default_proc=;T@›[I"delete;T@›[I"delete_if;T@›[I"dig;T@›[I" each;T@›[I"
each_key;T@›[I"each_pair;T@›[I"each_value;T@›[I"empty?;T@›[I" eql?;T@›[I"
fetch;T@›[I"fetch_values;T@›[I"flatten;T@›[I"
has_key?;T@›[I"has_value?;T@›[I" hash;T@›[I"
include?;T@›[I"inspect;T@›[I"invert;T@›[I"keep_if;T@›[I"key;T@›[I" key?;T@›[I" keys;T@›[I"length;T@›[I"member?;T@›[I"
merge;T@›[I"merge!;T@›[I"rassoc;T@›[I"rehash;T@›[I"reject;T@›[I"reject!;T@›[I"replace;T@›[I"select;T@›[I"select!;T@›[I"
shift;T@›[I" size;T@›[I"
store;T@›[I" to_a;T@›[I" to_h;T@›[I"to_hash;T@›[I"to_proc;T@›[I" to_s;T@›[I"transform_values;T@›[I"transform_values!;T@›[I"update;T@›[I"value?;T@›[I"values;T@›[I"values_at;T@›[;[ [;[ [ [U:RDoc::Context::Section[i 0o;;[ ;0;0[@“I"lib/pp.rb;T@>cRDoc::TopLevel