Jordon Bedwell
Jordon Bedwell

Reputation: 3247

Use Ruby hash value inside of hash

Is there a way to do something like this:

numbers = {
  "one" => "two",
  "three" => numbers["one"] }

I know I can just make the hash and set everything normally like numbers["one"] but ugly...

Upvotes: 0

Views: 102

Answers (1)

Ed Swangren
Ed Swangren

Reputation: 124642

No, because numbers is not yet defined, but you can make the items assigned more than once a variable:

# seems odd, but ok...
def_num = "two"
numbers = { 'one' => def_num, 'two' => def_num }

Upvotes: 1

Related Questions