Sign up or Sign in | Share
 
ClearMyBench
Welcome, Guest
Please Login or Register.    Lost Password?
Re:Ruby basics (1 viewing) (1) Guest
Go to bottom Post Reply Favoured: 0
TOPIC: Re:Ruby basics
#4475
ShriNi (User)
Software Techie (PEGA) currently on a Career Break
Platinum Boarder
Posts: 4000
graph
User Offline Click here to see the profile of this user
Gender: Female Location: Secundrabad India Birthdate: 1983-09-29
Re:Ruby basics 3 Years, 10 Months ago Karma: 0  
Hash Built-in Methods

We need to have an instance of Hash object to call a Hash method


for instance following is the way to create an instance of Hash object:

Hash[key =>|, value]* ] or

Hash.new [or] Hash.new(obj) [or]

Hash.new { |hash, key| block }


This will returns a new hash populated with the given objects.

Now using created object we can call any available instance methods
 
Report to moderator   Logged Logged  
  The administrator has disabled public write access.
#4476
ShriNi (User)
Software Techie (PEGA) currently on a Career Break
Platinum Boarder
Posts: 4000
graph
User Offline Click here to see the profile of this user
Gender: Female Location: Secundrabad India Birthdate: 1983-09-29
Re:Ruby basics 3 Years, 10 Months ago Karma: 0  
Let us discuss some of the public hash methods ( Assuming hash is an array object )



hash == other_hash

Tests whether two hashes are equal, based on whether they have the same number of key-value pairs, and whether the key-value pairs match the corresponding pair in each hash.
 
Report to moderator   Logged Logged  
  The administrator has disabled public write access.
#4477
ShriNi (User)
Software Techie (PEGA) currently on a Career Break
Platinum Boarder
Posts: 4000
graph
User Offline Click here to see the profile of this user
Gender: Female Location: Secundrabad India Birthdate: 1983-09-29
Re:Ruby basics 3 Years, 10 Months ago Karma: 0  
hash.[key]

Using a key, references a value from hash. If the key is not found, returns a default value.


hash.[key]=value


Associates the value given by value with the key given by key.
 
Report to moderator   Logged Logged  
  The administrator has disabled public write access.
#4478
ShriNi (User)
Software Techie (PEGA) currently on a Career Break
Platinum Boarder
Posts: 4000
graph
User Offline Click here to see the profile of this user
Gender: Female Location: Secundrabad India Birthdate: 1983-09-29
Re:Ruby basics 3 Years, 10 Months ago Karma: 0  
hash.clear

Removes all key-value pairs from hash.

hash.default(key = nil)

Returns the default value for hash, nil if not set by default=. (ash.clear

Removes all key-value pairs from hash.

hash.default(key = nil)

Returns the default value for hash, nil if not set by default=. ([] returns a default value if the key does not exist in hash.)
 
Report to moderator   Logged Logged  
  The administrator has disabled public write access.
#4479
ShriNi (User)
Software Techie (PEGA) currently on a Career Break
Platinum Boarder
Posts: 4000
graph
User Offline Click here to see the profile of this user
Gender: Female Location: Secundrabad India Birthdate: 1983-09-29
Re:Ruby basics 3 Years, 10 Months ago Karma: 0  
hash.default = obj

Sets a default value for hash.

hash.default_proc

Returns a block if hash was created by a block.
 
Report to moderator   Logged Logged  
  The administrator has disabled public write access.
#4480
ShriNi (User)
Software Techie (PEGA) currently on a Career Break
Platinum Boarder
Posts: 4000
graph
User Offline Click here to see the profile of this user
Gender: Female Location: Secundrabad India Birthdate: 1983-09-29
Re:Ruby basics 3 Years, 10 Months ago Karma: 0  
hash.inspect

Returns a pretty print string version of hash.

hash.invert

Creates a new hash, inverting keys and values from hash; that is, in the new hash, the keys from hash become values, and values become
keys.

hash.keys

Creates a new array with keys from hash.
 
Report to moderator   Logged Logged  
  The administrator has disabled public write access.
Go to top Post Reply
get the latest posts directly to your desktop
logo footer   Copyright 2009 - 2013 HotRecruiter.Com - All Rights Reserved