Determining type of an object in ruby

前端 未结 5 1167
情书的邮戳
情书的邮戳 2021-01-29 17:46

I\'ll use python as an example of what I\'m looking for (you can think of it as pseudocode if you don\'t know Python):

>>> a = 1
>>> type(a)
&l         


        
5条回答
  •  广开言路
    2021-01-29 18:04

    I would say "Yes". As "Matz" had said something like this in one of his talks, "Ruby objects have no types." Not all of it but the part that he is trying to get across to us. Why would anyone have said "Everything is an Object" then? To add he said "Data has Types not objects".

    So we might enjoy this.

    https://www.youtube.com/watch?v=1l3U1X3z0CE

    But Ruby doesn't care to much about the type of object just the class. We use classes not types. All data then has a class.

    12345.class
    
    'my string'.class
    

    They may also have ancestors

    Object.ancestors
    

    They also have meta classes but I'll save you the details on that.

    Once you know the class then you'll be able to lookup what methods you may use for it. That's where the "data type" is needed. If you really want to get into details the look up...

    "The Ruby Object Model"

    This is the term used for how Ruby handles objects. It's all internal so you don't really see much of this but it's nice to know. But that's another topic.

    Yes! The class is the data type. Objects have classes and data has types. So if you know about data bases then you know there are only a finite set of types.

    text blocks numbers

提交回复
热议问题