Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

more hash, array practice and multi line print with one statement

arr_1=["this", "that", "other", "and still, another"]
arr_2=[56, 9, "numbers", "creamy delights"]
test_hash_1={"stone"=>"agate", "animal"=>"capibarra"}

puts
puts test_hash_1
puts test_hash_1["stone"]
puts test_hash_1["animal"]
puts test_hash_1[arr_1] ### This should return an error, as I havent assigned a key to the value of 
### arr_1

puts
test_hash_2={3 => arr_1, 4 => arr_2, "animal" => "capibara", "stone" => "agate"}
puts
puts test_hash_2
puts test_hash_2["animal"]
puts test_hash_2["stone"]
puts test_hash_2[4]
puts test_hash_2[3]
puts
print <<eof
Apparently, a hash key can only be a number or a 
string, and not a letter variable, as letter variables 
return errors, while numbers and strings don't.

  And, it's worth mentioning, that this comment was printed 
  using the 'print <<eof' statement (function?).  'print <<eof'
  maintains formating, like the indent here.
eof

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.