Mercurial > repos > other > SevenLanguagesInSevenWeeks
changeset 15:ebb19742b98f
Add some minor experiments with hashes (hash tables)
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Tue, 03 Jan 2017 20:36:16 +0000 |
parents | 9aa1eba404f0 |
children | 8d46064c9afc |
files | 1-Ruby/hashes-irb.output 1-Ruby/hashes-ruby.output 1-Ruby/hashes.rb |
diffstat | 3 files changed, 36 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /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
--- /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
--- /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