Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Discovering Ruby object members?

What is an easy way to find out what methods/properties that a ruby object exposes?

As an example to get member information for a string, in PowerShell, you can do

"" | get-member 

In Python,

dir("") 

Is there such an easy way to discover member information of a Ruby object?

like image 751
dance2die Avatar asked Jan 20 '11 12:01

dance2die


People also ask

How do you find the object type in Ruby?

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.

How Ruby uses the object structure?

In `ruby`, the body of an object is expressed by a struct and always handled via a pointer. A different struct type is used for each class, but the pointer type will always be `VALUE` (figure 1). In practice, when using a `VALUE`, we cast it to the pointer to each object struct.

Why everything in Ruby is an object?

Practically everything in Ruby is an Object, with the exception of control structures. Whether or not under the covers a method, code block or operator is or isn't an Object, they are represented as Objects and can be thought of as such.


2 Answers

"foo".methods 

See:

http://ruby-doc.org/core/classes/Object.html

http://ruby-doc.org/core/classes/Class.html

http://ruby-doc.org/core/classes/Module.html

like image 186
noodl Avatar answered Oct 13 '22 23:10

noodl


Ruby doesn't have properties. Every time you want to access an instance variable within another object, you have to use a method to access it.

like image 39
Andrew Grimm Avatar answered Oct 14 '22 01:10

Andrew Grimm