[#377882] remove array bracket — Kamarulnizam Rahim <niezam54@...>

Hi when i run my script, the output is as followed:

18 messages 2011/02/02

[#378046] Setter method for Hash value — Rolf Pedersen <rolfhsp@...>

Hi

20 messages 2011/02/03
[#378052] Re: Setter method for Hash value — Brian Candler <b.candler@...> 2011/02/03

Rolf Pedersen wrote in post #979431:

[#378056] Re: Setter method for Hash value — Rolf Pedersen <rolfhsp@...> 2011/02/03

Hi Brian (and others who have contributed with suggestions along the same

[#378144] C extension: How to check if a VALUE is still alive (not being GC'ed)? — Iñaki Baz Castillo <ibc@...>

Hi, I'm coding an async DNS resolver for EventMachine based on udns (a

13 messages 2011/02/05
[#378171] Re: C extension: How to check if a VALUE is still alive (not being GC'ed)? — Tony Arcieri <tony.arcieri@...> 2011/02/06

On Sat, Feb 5, 2011 at 4:02 PM, I=F1aki Baz Castillo <ibc@aliax.net> wrote:

[#378179] Re: C extension: How to check if a VALUE is still alive (not being GC'ed)? — Iñaki Baz Castillo <ibc@...> 2011/02/06

2011/2/6 Tony Arcieri <tony.arcieri@medioh.com>:

[#378199] Choosing an office suite — Hilary Bailey <my77elephants@...>

I am trying to decide which office suite to choose from. The only

30 messages 2011/02/07
[#378229] Re: Choosing an office suite — Phillip Gawlowski <cmdjackryan@...> 2011/02/07

On Mon, Feb 7, 2011 at 11:15 AM, Hilary Bailey <my77elephants@gmail.com> wrote:

[#378202] making hash key from arrays — Arihan Sinha <arihan_sinha@...>

Hi All,

11 messages 2011/02/07

[#378254] "permission denied" happening too often — Peter Bailey <pbailey@...>

Hello,

15 messages 2011/02/08
[#378256] Re: "permission denied" happening too often — Anurag Priyam <anurag08priyam@...> 2011/02/08

> I've got Ruby scripts that have been working fine for years now. But,

[#378257] Re: "permission denied" happening too often — Markus Schirp <mbj@...> 2011/02/08

You can also try to strace your script. In the logs you'll find the system

[#378259] Re: "permission denied" happening too often — Peter Bailey <pbailey@...> 2011/02/08

Markus Schirp wrote in post #980289:

[#378307] undefined class/module YAML::PrivateType - Error — "Priya D." <dharsininitt@...>

Hi,

11 messages 2011/02/09

[#378341] System calls with ` in parameters — "Gerad S." <geradstemke@...>

Hi All,

12 messages 2011/02/09

[#378618] Defining class methods — Tony Arcieri <tony.arcieri@...>

It seems there are 3 ways of defining class methods (at least in common

12 messages 2011/02/16

[#378685] LiveAST: a pure Ruby 1.9.2 library obtaining live abstract syntax trees — "James M. Lawrence" <quixoticsycophant@...>

= LiveAST

13 messages 2011/02/18

[#378753] posix_mq : Problem installing on HPUX — Tadeusz Bochan <tad.bochan@...>

Hello,

13 messages 2011/02/20

[#378890] a, b = Array.new(2).map!{|x| data.dup} — Stefan Salewski <mail@...>

I think I can replace this code

19 messages 2011/02/23
[#378892] Re: a, b = Array.new(2).map!{|x| data.dup} — niklas | brueckenschlaeger <niklas@...> 2011/02/23

Are you sure you can't rework your code to *not* copy data 5x? I assume

[#378899] Re: a, b = Array.new(2).map!{|x| data.dup} — Stefan Salewski <mail@...> 2011/02/23

On Thu, 2011-02-24 at 07:00 +0900, niklas | brueckenschlaeger wrote:

[#378941] Automatic question generator libs in Ruby Language — Sniper Abandon <sathish.salem.1984@...>

is there any Automatic question generator libraries in Ruby Language ?

20 messages 2011/02/24
[#379058] Re: Automatic question generator libs in Ruby Language — Sniper Abandon <sathish.salem.1984@...> 2011/02/27

suppose if i have a paragraph (arround 250 words)

[#379172] Re: Automatic question generator libs in Ruby Language — Shadowfirebird <shadowfirebird@...> 2011/03/01

> i want to get all the possible question from that paragraph

[#379174] Re: Automatic question generator libs in Ruby Language — Peter Zotov <whitequark@...> 2011/03/01

On Tue, 1 Mar 2011 19:31:36 +0900, Shadowfirebird wrote:

[#379175] Re: Automatic question generator libs in Ruby Language — Adam Prescott <mentionuse@...> 2011/03/01

On Tue, Mar 1, 2011 at 10:55 AM, Peter Zotov <whitequark@whitequark.org>wrote:

[#379177] Re: Automatic question generator libs in Ruby Language — Peter Zotov <whitequark@...> 2011/03/01

On Tue, 1 Mar 2011 20:02:13 +0900, Adam Prescott wrote:

[#379179] Re: Automatic question generator libs in Ruby Language — Adam Prescott <mentionuse@...> 2011/03/01

On Tue, Mar 1, 2011 at 12:28 PM, Peter Zotov <whitequark@whitequark.org>wrote:

[#378949] why is $1 in a grep() equal to nil? — 7stud -- <bbxx789_05ss@...>

class DataSource

16 messages 2011/02/24
[#378953] Re: why is $1 in a grep() equal to nil? — Eric Christopherson <echristopherson@...> 2011/02/25

On Thu, Feb 24, 2011 at 2:59 PM, 7stud -- <bbxx789_05ss@yahoo.com> wrote:

[#378958] parsing rule for this code? — 7stud -- <bbxx789_05ss@...>

1)

11 messages 2011/02/25

[#379000] Symbol#to_proc helping out with #select to beat Scala-s solution — Jarmo Pertman <jarmo.p@...>

Hey!

9 messages 2011/02/25

[#379074] finding a tag in a binary file — rob stanton <tnotnats@...>

I have a binary file in which I'd like to find multiple strings of 10

12 messages 2011/02/27

Re: Ruby Hash Keys and Related Questions

From: Josh Cheek <josh.cheek@...>
Date: 2011-02-23 07:43:58 UTC
List: ruby-talk #378859
On Wed, Feb 23, 2011 at 12:42 AM, Terry Michaels <cmhoward@frigidcode.com>wrote:

> I'm still a bit new to Ruby, so humor me a bit. But I discovered today
> (through trial and error) that not only can Strings, numbers, and
> symbols be keys for hashes, but also any object, or even a class name!
> Ruby is the first language I've used in which I would have even thought
> to try that, let alone it actually working:
>
>
> irb(main):001:0> hsh = {}
> => {}
> irb(main):002:0> obj = Object.new
> => #<Object:0x7fa4b83ab1f0>
> irb(main):003:0> obj2 = Object.new
> => #<Object:0x7fa4b83a7320>
> irb(main):004:0> hsh[obj] = "blah"
> => "blah"
> irb(main):005:0> hsh[obj2] = "ble"
> => "ble"
> irb(main):006:0> puts hsh[obj2]
> ble
> => nil
> irb(main):007:0> puts hsh[obj1]
> NameError: undefined local variable or method `obj1' for main:Object
>  from (irb):7
> irb(main):008:0> puts hsh[obj]
> blah
> => nil
> irb(main):009:0> clone = obj
> => #<Object:0x7fa4b83ab1f0>
> irb(main):010:0> puts hsh[clone]
> blah
> => nil
> irb(main):011:0> class Cl
> irb(main):012:1> end
> => nil
> irb(main):013:0> hsh[Cl] = "blo"
> => "blo"
> irb(main):014:0> puts hsh[obj]
> blah
> => nil
> irb(main):015:0> puts hsh[Cl]
> blo
> => nil
> irb(main):016:0> class Cl2
> irb(main):017:1> end
> => nil
> irb(main):018:0> hsh[Cl2] = "blu"
> => "blu"
> irb(main):019:0> puts hsh[Cl]
> blo
> => nil
> irb(main):020:0> puts hsh[Cl2]
> blu
>
> Anyway, this raised a few related questions in my mind:
>
> 1. If the "key" taken by hash[key]= can be any object, and the key still
> works even after it is aliased to another variable, does that mean that
> the "key" is just a reference?
>
>
The key can be any object that implements the methods "hash" and "eql?" I'm
not sure what you mean when you say "does that mean the 'key' is just a
reference?" If you are asking what Ruby is passing around, the answer is "a
pointer to the object". That is less interesting in this case, the more
interesting thing is why it is behaving that way, which is that objects have
hash defined on them, which returns their object_id

o = Object.new
o.hash        # => 2154796
o.object_id   # => 2154796

I don't know if you know how hashes are implemented, but internally they map
objects to array indexes. The way Ruby does this is with the hash method,
which returns a number that correlates to the index. In your example with
obj and obj2, you can store different values there. But they are both just
empty objects, does it make sense to consider them two different keys or the
same key? With objects like this, they get different keys because they will
have different object ids. But think about a String, where each string is
different.

a1 = 'a'
a2 = 'a'
a1.object_id  # => 2153018
a2.object_id  # => 2153004

Do you want to have to always keep track of which string you used as the
key? No. So the hash for a string is based on the string value, in this case
"a", rather than the specific instance of "a" that was used to put it into
the hash.

a1.hash       # => 14815807
a2.hash       # => 14815807


2. If I pass in a number, say an Integer, as a key, does Ruby actually
> use the Integer? Or does it use a reference to an Integer object?
> (Numbers are objects too, right?)
>
>
There have been long discussions about this, Caleb Clousen tells me that
Fixnums are copied every time they are passed as an argument. He has gone
much deeper than I have, so presumably he knows what he is talking about,
but Ruby goes to really great lengths to hide this from you, to the point
that you must construct contrived explanations to handle the contradictions
that such models have.

I think it is best to Just consider every variable a pointer to the object.
Fixnum or not.


> 3. If I am allowed to pass in a class as a key, does that mean that
> classes are objects too? If not, what exactly is being stored as the
> key?
>
>
Yes, classes are objects:

class C
end

c = C
c == C      # => true
C.class     # => Class
C.object_id # => 2156420
C.hash      # => 2156420

classes = [C,Array,String]
classes # => [C, Array, String]

Notice that they inherit the default hash method that just uses their object
id as the hash key.


> 4. When I use irb, and a line returns an object, irb shows me the
> object's hexadecimal reference address (or at least, that's what it
> looks like). Is there a method one can call on an object to get that
> reference when one is not in irb? Just curious.
>
>
object.object_id

-----

If you're interested, here is about as simple of an implementation of a hash
table as you can get https://gist.github.com/840135

The purpose is to conceptually understand that hashes internally use arrays,
and see a simple example of how they achieve this. Real hashes are much more
complex (ie what happens if two objects hash to the same value? what happens
if two different objects should be considered the same hash key? what
happens when the array gets full? how is the #hash method written? etc.)

In This Thread