Yung Sifilis
Yung Sifilis

Reputation: 118

Print specified value from hash Ruby

I'd like to know how get the specified field from Hash.

{"codeid"=>120023, "eppocode"=>"1BOBO", "prefname"=>"Pikant", "level"=>7}

I have this Hash and i want to get level field (7) becouse i need to compare it afterwards Thanks!

Upvotes: 0

Views: 54

Answers (2)

Todd A. Jacobs
Todd A. Jacobs

Reputation: 84443

Using Standard Hash Accessors

You can use either Hash#[] or Hash#dig for this. There are other ways to do this too such as pattern matching, but those are overkill for what you're doing.

For example:

h = {
  "codeid"   => 120023,
  "eppocode" => "1BOBO",
  "prefname" => "Pikant",
  "level"    => 7,
}

h["level"]
#=> 7

h.dig "level"
#=> 7

Upvotes: 1

spickermann
spickermann

Reputation: 107107

Like this:

hash = {"codeid"=>120023, "eppocode"=>"1BOBO", "prefname"=>"Pikant", "level"=>7}
hash["level"]
#=> 7

Upvotes: 0

Related Questions