Confusion with has object creation

I know we create Hash object with Hash.new,it calls `initialize` method.

Here it is:

class Hash
  alias_method :_initialize, :initialize
  def initialize
     puts 'welcome!!'
     _initialize
  end
end
Hash.new #welcome

But how hash object is created when we call them as below:

Hash['a', 'b']
Hash['a' => 'b']

What method is being called internally?

···

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

Hash[] only call methods that you can not overwrite... it allocates the
Hash without calling initialize.

Hash[obj] with only one argument does some kind of Magic too (because it
trys to convert into an Hash and if that fails, try to convert into
Array)

···

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

last time you used C functions to show me that i am Wrong and now you
want C code? okay you get it:

when you do "show source" at
http://www.ruby-doc.org/core-2.0/Hash.html#method-c-5B-5D you get this:

please notice the: rb_hash_s_try_convert and rb_check_array_type

except for this ones the Hash[] function does not call ruby methods and
only use the C representations of them, so overwriting them in ruby does
not have any form of effect

···

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

This is the method

You can indeed overwrite it:

irb(main):001:0> class Hash
irb(main):002:1> def self.(*args)
irb(main):003:2> {}
irb(main):004:2> end
irb(main):005:1> end
=> nil
irb(main):006:0> Hash['1','2']
=> {}

···

On Wed, Mar 20, 2013 at 12:01 AM, Hans Mackowiak <lists@ruby-forum.com>wrote:

Hash only call methods that you can not overwrite... it allocates the
Hash without calling initialize.

Hash[obj] with only one argument does some kind of Magic too (because it
trys to convert into an Hash and if that fails, try to convert into
Array)

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

Hans Mackowiak wrote in post #1102398:

Hash only call methods that you can not overwrite... it allocates the
Hash without calling initialize.

Please give me one code example of this.I am heavily interested on it. I
tried some code to see it,but not worked as I am looking for.

···

Hash[obj] with only one argument does some kind of Magic too (because it
trys to convert into an Hash and if that fails, try to convert into
Array)

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

Han s Mackowiak wrote in post #1102404:

last time you used C functions to show me that i am Wrong and now you
want C code? okay you get it:

No friend! I didn't challenge you, i will never do that even if I got
grow up in ruby,rather I will try to convince him with the reality.:slight_smile:
Now I am not grown up so much in Ruby only 50% by concept, but I will
try reach to 80%.

As far as I know I tried to show you my confusion areas, after that I
drived into the source code with your hints and solved my confusion.

In this case,I am also looking into the source code,but didn't getting
it as I am not getting function description of any function ruby till
now,only code I got. within code to much functions are called. Those
functions leading me to another confusion area, instead that if I got
proper word description of each function,that could be better for me.
like say a below.

My func(){
add(x,y) /* this function adds to number*/
}

Above kind of flavor I am not getting there. :slight_smile: :slight_smile:

···

when you do "show source" at
Class: Hash (Ruby 2.0.0) you get this:

https://github.com/ruby/ruby/blob/trunk/hash.c#L389

please notice the: rb_hash_s_try_convert and rb_check_array_type

except for this ones the Hash function does not call ruby methods and
only use the C representations of them, so overwriting them in ruby does
not have any form of effect

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