[#401849] If statement — Masoud Ahmadi <lists@...>

Will anyone be able to point out what I am doing wrong.

15 messages 2012/12/02

[#401987] Trying to get "translator" to work — JD KF <lists@...>

So, basically, I'm trying to get the below code to work properly for

12 messages 2012/12/06

[#402012] Need help to select some listbox item in different listbox together — Jonathan Masato <lists@...>

Hello,

10 messages 2012/12/07

[#402045] if n belongs to set a and m belongs to set b repeat some steps, How? — "zubair a." <lists@...>

We can do so in java and similar languages like:

11 messages 2012/12/08

[#402078] Time.new(2001, 12, 3).to_i returns wrong value — Robert Buck <lists@...>

I am doing something that not many do, I am writing a database driver

9 messages 2012/12/09

[#402145] How I can create/extract a variable/hash into the current binding in Ruby? — Ramon de C Valle <rcvalle@...>

Hi,

12 messages 2012/12/12

[#402205] Wondering About Flatiron School — "Kevin Y." <lists@...>

Hi everyone!,

35 messages 2012/12/15
[#402207] Re: Wondering About Flatiron School — Chad Perrin <code@...> 2012/12/15

On Sat, Dec 15, 2012 at 11:51:08AM +0900, Kevin Y. wrote:

[#402214] Ruby quick reference arranged in ASCII sequence? — Old Grantonian <lists@...>

As a ruby beginner, I would be grateful for any links to a ruby

17 messages 2012/12/15

[#402226] print - and strip text between tags using Nokogiri — Paul Mena <lists@...>

I'm a Ruby Newbie trying to write a program to process thousands of HTML

13 messages 2012/12/15

[#402332] Perl to Ruby: regex captures to assignment. — "Derrick B." <lists@...>

Hello all,

37 messages 2012/12/19
[#402342] Re: Perl to Ruby: regex captures to assignment. — "Derrick B." <lists@...> 2012/12/20

First of all, thanks for the fast responses!

[#402352] Re: Perl to Ruby: regex captures to assignment. — Robert Klemme <shortcutter@...> 2012/12/20

On Thu, Dec 20, 2012 at 1:38 AM, Derrick B. <lists@ruby-forum.com> wrote:

[#402357] Re: Perl to Ruby: regex captures to assignment. — "Derrick B." <lists@...> 2012/12/20

Robert Klemme wrote in post #1089733:

[#402359] trying to strip characters from a line — Paul Mena <lists@...>

I'm reading a table from a MySQL database and then processing it row by

18 messages 2012/12/20

[#402394] simple division: -9 / 5 = -2 what? — "Derrick B." <lists@...>

$ irb

13 messages 2012/12/22

[#402412] POLS and string-handling — Paul Magnussen <lists@...>

Hi,

14 messages 2012/12/22

[#402460] "Open" dialog of Windows — "Damián M. González" <lists@...>

Hi guys, been researching about pop up the "open" file dialog of

11 messages 2012/12/24

[#402466] How do I install Ruby on my Ubuntu 12.10 partition. — Kaye Ng <lists@...>

I already have Ruby installed on my Windows 7 partition.

23 messages 2012/12/25

[#402510] Ruby Association Certified Ruby Programmer — Sean Westfall <lists@...>

How well respected is this certification in the industry: Ruby

27 messages 2012/12/27
[#402528] Re: Ruby Association Certified Ruby Programmer — Peter Hickman <peterhickman386@...> 2012/12/27

On 27 December 2012 01:28, Sean Westfall <lists@ruby-forum.com> wrote:

[#402555] numeric? — Brandon Weaver <keystonelemur@...>

I've found a bit of an annoyance trying to find out if a number is numeric

20 messages 2012/12/27

[#402580] Ruby Koans regarding Hashes. — "Derrick B." <lists@...>

I am trying to understand this, so let me know how I do. :) I know

18 messages 2012/12/28

[#402609] can't open new ruby program under "new" context menu — "Lee V." <lists@...>

I'm stuck on the new version at trying to do something very simple.

10 messages 2012/12/28

[#402642] require "test/unit" — "Mattias A." <lists@...>

Hi,

17 messages 2012/12/29
[#402667] Re: require "test/unit" — "Mattias A." <lists@...> 2012/12/31

Hi Dami=C3=A1n M. Gonz=C3=A1lez!

[#402747] Re: require "test/unit" — "Derrick B." <lists@...> 2013/01/04

Mattias A. wrote in post #1090700:

[#402749] Re: require "test/unit" — sto.mar@... 2013/01/04

Am 04.01.2013 19:48, schrieb Derrick B.:

Ruby Koans regarding Hashes.

From: "Derrick B." <lists@...>
Date: 2012-12-28 05:27:43 UTC
List: ruby-talk #402580
I am trying to understand this, so let me know how I do.  :)  I know
this is probably a breeze for you veterans, but I need to put this in
words.  heh

I have a C++/Perl background, so I do understand "associative arrays",
but understanding Ruby hash objects, and the methods and operations that
can be used with them is pretty new to me.

In the "Ruby Koans" file, "about_hashes.rb", I completed this method.
It was just a little tough to understand, initially, but I think I got
it.  Here's the test method with helpful line numbers:

  def test_default_value_is_the_same_object
1.    hash = Hash.new([])

2.    hash[:one] << "uno"
3.    hash[:two] << "dos"

4.    assert_equal ["uno", "dos"], hash[:one]
5.    assert_equal ["uno", "dos"], hash[:two]
6.    assert_equal ["uno", "dos"], hash[:three]

7.    assert_equal true, hash[:one].object_id == hash[:two].object_id
  end

Here is my breakdown:

1.  a new hash is created, utilizing an empty array as the default value
for any key created without a value, and assigned to "hash".  This
wasn't too bad, I just had to look up: Hash.new(<place a default key
value here>)

2.  using indexing, the string, "uno", is appended to the default key
value for the key, :one, which was initially an empty array and is now
the value ["uno"].  Since the "<<" was used instead of the assignment
operator "=", it is an append, rather than an assign.  For example, if
the statement was "hash[:one] = "uno", then that replaces the default
array and the value is now a String

3. same as 2., but what I found interesting was how a new key was used,
and the append still modified the general default value for the entire
hash!  So, now the key value default is now: ["uno", "dos"]

4 - 6  test cases to prove that a default key value was created, with
lines 2 and 3 appending to it, and because these were not assignment
expressions, the keys tested (:one, :two, :three) are using the modified
default value.

7.  Final proof that is the same default value, or same Array object_id,
being used for the two utilized keys.

So, when you create a Hash object, you can provide a default value.  In
this case, the default value is an Array object.  Because of this being
an Array object, the "<<" method can be used to modify the default
value.  Using indexing on the Hash, this can be tested by querying the
hash:

hash = Hash.new([])
=> {}
hash[:foo]
=> []
hash[:foo] << "hello"
=> ["hello"]
hash[:foo]
=> ["hello"]
hash.inspect
=> {}

The last line "hash.inspect" is what I also find interesting, because if
there are default values, why is it still empty?  I then assign a value,
overriding the default value:

hash[:foo] = "bar"
=> "bar"
hash.inspect
=> "{:foo=>\"bar\"}"

Now, inspect no longer shows an empty hash.  Hmmm...

Ruby hashes are an interesting topic and I look forward to your veteran
insight on this topic.

Thanks!

-- 
Posted via http://www.ruby-forum.com/.

In This Thread

Prev Next