Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 290 Vote(s) - 3.42 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Determining type of an object in ruby

#1
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)
<type 'int'>

I know in ruby I can do :

1.9.3p194 :002 > 1.class
=> Fixnum

But is this the proper way to determine the type of the object?
Reply

#2
Oftentimes in Ruby, you don't actually care what the object's class is, per se, you just care that it responds to a certain method. This is known as [Duck Typing][1] and you'll see it in all sorts of Ruby codebases.

So in many (if not most) cases, its best to use Duck Typing using [`#respond_to?(method)`][2]:

object.respond_to?(:to_i)

[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#3
you could also try: [`instance_of?`](

[To see links please register here]

)

p 1.instance_of? Fixnum #=> True
p "1".instance_of? String #=> True
p [1,2].instance_of? Array #=> True
Reply

#4
The proper way to determine the "type" of an object, which is a wobbly term in the Ruby world, is to call `object.class`.

Since classes can inherit from other classes, if you want to determine if an object is "of a particular type" you might call `object.is_a?(ClassName)` to see if `object` is of type `ClassName` or derived from it.

Normally type checking is not done in Ruby, but instead objects are assessed based on their ability to respond to particular methods, commonly called "[Duck typing](

[To see links please register here]

;. In other words, if it responds to the methods you want, there's no reason to be particular about the type.

For example, `object.is_a?(String)` is too rigid since another class might implement methods that convert it into a string, or make it behave identically to how String behaves. `object.respond_to?(:to_s)` would be a better way to test that the object in question does what you want.
Reply

#5
variable_name.class

Here variable name is "a"
`a.class`
Reply

#6
every variable have a prop with name class. if you print it, it will tell you what type it is. so do like this:

puts a.class

Reply

#7
I would say "yes".
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".


[RubyConf 2016 - Opening Keynote by Yukihiro 'Matz' Matsumoto
][1]

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

12345.class

'my string'.class

Classes 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


[1]:
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through