Created
February 9, 2023 13:22
-
-
Save composerinteralia/0b9681fe5afe8f6b7f40df79652b54ca to your computer and use it in GitHub Desktop.
Ruby symbol types
This file contains 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
def type(obj) | |
case obj.object_id | |
when 0 | |
:false | |
when 8 | |
:nil | |
when 20 | |
:true | |
when -> { _1[0] == 1 } | |
:integer | |
when -> { _1[1] == 1 } | |
:float | |
when -> { _1 % 20 == 8 } | |
id = obj.object_id / 20 | |
case id[1..3] | |
when 0 then :static_sym | |
when 1 then :static_ivar_sym | |
when 3 then :static_global_sym | |
when 4 then :static_attrset_sym | |
when 5 then :static_const_sym | |
when 6 then :static_civar_sym | |
when 7 then :static_junk_sym | |
else | |
raise "WTF" | |
end | |
else | |
:heap | |
end | |
end | |
p 10.times.map { Object.new.object_id } | |
p 10.times.map { Object.new.object_id.object_id ^ 1 } | |
p type Object.new | |
p type :"dynamic#{"sym"}" | |
p type :sym | |
p type :@sym | |
p type :$sym | |
p type :sym= | |
p type :Sym | |
p type :@@sym | |
p type :"-sym" | |
p type 1 | |
p type 1.0 | |
p type true | |
p type false | |
p type nil |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment