Created
December 22, 2010 06:52
-
-
Save deepak/751192 to your computer and use it in GitHub Desktop.
fixnum instance does not have a eigenclass
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- coding: utf-8 -*- | |
| # $ ruby -vw foobar.rb | |
| # ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-linux] | |
| # foobar.rb:1: syntax error, unexpected tINTEGER | |
| # def 1.hello | |
| # ^ | |
| # foobar.rb:3: syntax error, unexpected keyword_end, expecting $end | |
| class Object | |
| def metaclass | |
| class << self; self; end | |
| end | |
| end | |
| # Fixnum objects do not have metaclass | |
| begin | |
| binding.eval("def 1.hello;puts \"hello #{self}\";end") #Error: defining hello on 1 did not work | |
| rescue Exception | |
| puts "Error: defining hello on 1 did not work" #noop | |
| end | |
| puts 1.class #Fixnum | |
| puts 1.metaclass rescue nil #Error, fixnum instance does not have a metaclass. can't define singleton (TypeError) | |
| puts 1.class.metaclass # #<Class:Fixnum> | |
| puts 1.class.metaclass.object_id == 1.class.object_id #false | |
| puts 1.class.metaclass.object_id == 1.class.class.object_id #false | |
| class Fixnum | |
| def hello | |
| puts "hello #{self}" | |
| end | |
| end | |
| 1.hello #hello 1 | |
| 1.instance_eval { @x = 10 } | |
| puts 1.instance_eval { @x } #10 | |
| __END__ | |
| dkannan> why cannot i define a method in the eigenclass of a instance of a Fixnum? 11:16 AM | |
| <cdcarter> fixnums don't have eigenclasses 11:17 AM | |
| <dkannan> really. let me check 11:19 AM | |
| <cdcarter> TypeError: no virtual class for Fixnum 11:20 AM | |
| <banisterfiend> cdcarter: what did you type to get that error? 11:21 AM | |
| <banisterfiend> and what ruby version are you on? 11:21 AM | |
| <cdcarter> class<<3;self;end 11:21 AM | |
| <cdcarter> on 1.8, i should note 11:21 AM | |
| <banisterfiend> yeah 11:21 AM | |
| <banisterfiend> 1.9 has a diff error msg 11:21 AM | |
| <dkannan> can't define singleton (TypeError) in 1.9.2-p0 11:22 AM | |
| <banisterfiend> dkannan: fixnums can have ivars though 11:22 AM | |
| <dkannan> banisterfiend: 1.instance_eval { @x = 10 }; 1.instance_eval { @x }; 11:23 AM | |
| <dkannan> banisterfiend: how does this work, meaning where is it stored 11:24 AM | |
| <banisterfiend> dkannan: in an ivar table 11:24 AM | |
| <banisterfiend> dkannan: where did you think? :P 11:26 AM | |
| <banisterfiend> but ok 11:26 AM | |
| <banisterfiend> ordinary T_OBJECTs and T_CLASSes and so on have pointers to an ivar table in their structs 11:26 AM | |
| <banisterfiend> immediate values (like fixnums) dont, instead their ivar tables are associated to them through a hash table 11:27 AM | |
| <banisterfiend> called a 'generic_ivar_table' or some such 11:27 AM | |
| <banisterfiend> you pass it the immediate value, and it returns the ivar table for that immedate 11:27 AM | |
| <banisterfiend> immediate 11:27 AM | |
| <banisterfiend> same with T_DATA objects 11:27 AM | |
| <banisterfiend> they all have teh EXIVAR flag set 11:27 AM | |
| <banisterfiend> the* 11:27 AM | |
| <banisterfiend> which means 'this object has an external ivar table, look in the generic ivar table to get it' 11:28 AM | |
| <cdcarter> T_DATAs don't have their own ivar table? 11:28 AM | |
| <banisterfiend> im pretty sure they dont 11:28 AM | |
| <cdcarter> maybe I don't remember my C API fully 11:29 AM | |
| <cdcarter> i thought they were VALUEs that came from Data_Wrap_Struct 11:29 AM | |
| <banisterfiend> yeah 11:29 AM | |
| <cdcarter> oh right 11:30 AM | |
| <cdcarter> they wrap the struct 11:30 AM | |
| <cdcarter> so the struct doesn't have an ivar member 11:30 AM | |
| <cdcarter> right right right 11:30 AM | |
| → dhruvasagar (~dhruvasag@118.102.165.194) joined 11:30 AM | |
| <dkannan> is this because these values are interned - the datastructure is in a hashmap rather than in that instance 11:30 AM | |
| <dkannan> 1.object_id == 1.object_id => true 11:31 AM | |
| <banisterfiend> dkannan: the values dont have structs associated with them, they're burnt into the VALUE itself, so there's no room for an iv table 11:31 AM | |
| <dkannan> what if i want to send a message to a fixnum - but i want it to behave differently based on the value 11:33 AM | |
| <banisterfiend> then define the method on Fixnum 11:33 AM | |
| <banisterfiend> and have a condition inside the method 11:33 AM | |
| <banisterfiend> if self == blah 11:33 AM | |
| <dkannan> oh ok 11:34 AM |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment