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?
on 2013-03-20 07:37
on 2013-03-20 08:01
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)
on 2013-03-20 08:12
This is the method http://www.ruby-doc.org/core-2.0/Hash.html#method-c-5B-5D 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 2013-03-20 08:13
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)
on 2013-03-20 08:25
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: 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
on 2013-03-20 08:36
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.:) 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. :) :) > when you do "show source" at > http://www.ruby-doc.org/core-2.0/Hash.html#method-c-5B-5D 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
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.