# HG changeset patch # User IBBoard # Date 1483475776 0 # Node ID ebb19742b98fa3662decfb62fd819e6a0caeaab9 # Parent 9aa1eba404f0a0304ff447c2bf437aa6720448f6 Add some minor experiments with hashes (hash tables) diff -r 9aa1eba404f0 -r ebb19742b98f 1-Ruby/hashes-irb.output --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/1-Ruby/hashes-irb.output Tue Jan 03 20:36:16 2017 +0000 @@ -0,0 +1,21 @@ +hashes.rb(main):001:0> #! /usr/bin/env ruby +hashes.rb(main):002:0* +hashes.rb(main):003:0* # Ruby has inline hashmap definitions, like PHP. +hashes.rb(main):004:0* # Java doesn't. C# isn't quite as clear as this "key => value" pattern. +hashes.rb(main):005:0* frenchify = { 'one' => 'un', 'two' => 'deux' } +=> {"one"=>"un", "two"=>"deux"} +hashes.rb(main):006:0> puts frenchify['one'] +un +=> nil +hashes.rb(main):007:0> +hashes.rb(main):008:0* # Or, done properly with symbols: +hashes.rb(main):009:0* frenchify = { :one => 'un', :two => 'deux' } +=> {:one=>"un", :two=>"deux"} +hashes.rb(main):010:0> puts frenchify[:one] +un +=> nil +hashes.rb(main):011:0> # But what is a symbol? Its own class, apparently! +hashes.rb(main):012:0* puts :one.classhashes.rb(main):012:0* +hashes.rb(main):012:0> +Symbol +=> nil diff -r 9aa1eba404f0 -r ebb19742b98f 1-Ruby/hashes-ruby.output --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/1-Ruby/hashes-ruby.output Tue Jan 03 20:36:16 2017 +0000 @@ -0,0 +1,3 @@ +un +un +Symbol diff -r 9aa1eba404f0 -r ebb19742b98f 1-Ruby/hashes.rb --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/1-Ruby/hashes.rb Tue Jan 03 20:36:16 2017 +0000 @@ -0,0 +1,12 @@ +#! /usr/bin/env ruby + +# Ruby has inline hashmap definitions, like PHP. +# Java doesn't. C# isn't quite as clear as this "key => value" pattern. +frenchify = { 'one' => 'un', 'two' => 'deux' } +puts frenchify['one'] + +# Or, done properly with symbols: +frenchify = { :one => 'un', :two => 'deux' } +puts frenchify[:one] +# But what is a symbol? Its own class, apparently! +puts :one.class \ No newline at end of file