Ruby introspection????

R

r

Hello all,
I come from python world wishing to learn Ruby. Many small things are
impeading my learning process. Could someone show me the equivilant
methods in Ruby

warning python syntax below :)

1.) dir(obj) -> <returns list of all names in scope>

2.) help(obj) -> <no brainer>

3.) print obj.__doc__ --> <docstring>

4.) type(obj)

5.) id(obj)

any other helpful methods??
 
A

Avdi Grimm

1.) dir(obj) -> <returns list of all names in scope>
obj.instance_methods

2.) help(obj) -> <no brainer>

Doesn't exist. Ruby does not have documentation metadata attached to objects.
3.) print obj.__doc__ --> <docstring>

See above.
4.) type(obj)
obj.class

5.) id(obj)

obj.id


You may notice a trend here - where in Python you sometimes use
method(obj) and sometimes obj.method() to find out about an object, in
Ruby it's *always* obj.method().
any other helpful methods??

http://ruby-doc.org/

--
Avdi

Home: http://avdi.org
Developer Blog: http://avdi.org/devblog/
Twitter: http://twitter.com/avdi
Journal: http://avdi.livejournal.com
 
R

Robert Klemme

Doesn't exist. Ruby does not have documentation metadata attached to objects.

Although it can be added easily

irb(main):001:0> class Module
irb(main):002:1> def doc(s=nil) s ? @doc = s : @doc end
irb(main):003:1> end
=> nil
irb(main):004:0> class Object
irb(main):005:1> def help; self.class.doc; end
irb(main):006:1> end
=> nil
irb(main):007:0> class Foo
irb(main):008:1> doc "silly example"
irb(main):009:1> end
=> "silly example"
irb(main):010:0> f = Foo.new
=> #<Foo:0x7ff740e8>
irb(main):011:0> f.help
=> "silly example"
irb(main):012:0>

Note that #id is deprecated, rather use obj.object_id.

Kind regards

robert
 
R

Rados³aw Bu³at

But it doesn't change situation for core/stdlib classes. Python's
__doc__ is very useful, specially when someone is learning language
and playing with it interactive shell. For Ruby I have defined method
'ri' in my ~/.irbrc file:

def ri(*names)
system("ri %s" % names.map(&:to_s).join(" "))
end

so I can type in irb: ri 'Array#zip' and rdoc documentation is
displayed. IMHO __doc__ is better because for nested calls
(obj.method1().method2().attribute) in ruby you should first know what
class you have (ok you don't but type 'ri zip' and you get 20
different choices).

--=20
Pozdrawiam

Rados=B3aw Bu=B3at
http://radarek.jogger.pl - m=F3j blog
 
M

Michael Fellinger

2009/1/10 Rados=C5=82aw Bu=C5=82at said:
But it doesn't change situation for core/stdlib classes. Python's
__doc__ is very useful, specially when someone is learning language
and playing with it interactive shell. For Ruby I have defined method
'ri' in my ~/.irbrc file:

def ri(*names)
system("ri %s" % names.map(&:to_s).join(" "))
end

so I can type in irb: ri 'Array#zip' and rdoc documentation is
displayed. IMHO __doc__ is better because for nested calls
(obj.method1().method2().attribute) in ruby you should first know what
class you have (ok you don't but type 'ri zip' and you get 20
different choices).

Actually, irb ships with a help method already, just seems nobody
knows about it:

sigma ~ % irb
method :help
# #<Method: Object(IRB::ExtendCommandBundle)#help>
help :Method

http://github.com/rubyspec/matzruby/tree/trunk/lib/irb/cmd/help.rb

^ manveru
 
R

r

But it doesn't change situation for core/stdlib classes. Python's
__doc__ is very useful, specially when someone is learning language
and playing with it interactive shell. For Ruby I have defined method
'ri' in my ~/.irbrc file:


I must say that learning Ruby can be very painful at times. Python doc
strings, and help() function are lifesavers. I am trying to keep an
open mind, but i find myself beating my head against the desk at
times. :). Every language needs doctrings and some sort of shell docs
for new users. Ruby is not as noob friendly as it should be.

[Avdi]
You may notice a trend here - where in Python you sometimes use method
(obj) and sometimes obj.method() to find out about an object, in Ruby
it's *always* obj.method().
[/Avdi]

Python has a set of built-in functions like -- help(), id(), type(),
isinstance(), dir(), and many more. I rather like the way procedural
style is supported and scoping is handled. A noob can ease into OOP.
Don't get me wrong, i LOVE OOP, but forcing OOP on someone from the
beginning can be quite confusing. And besides there are many problems
where the OOP machinery just cannot be justified, and is overkill.
Procedural style lends itself to many problems in a simplistic way --
IMO -- and helps a new user navigate the language semantics in a more
strait forward way.
 
A

Avdi Grimm

2009/1/10 r said:
A noob can ease into OOP.
Don't get me wrong, i LOVE OOP, but forcing OOP on someone from the
beginning can be quite confusing.

I came to Ruby from an OO background, and found it perfectly
comprehensible. You came to Ruby from a more procedural background.
That's fine, but don't mistake your confusion at changing paradigms
for inherent complexity. Anyone gets confused when they try to
rearrange their thoughts after having already settled into a certain
model. For instance, it took me some time to wrap my mind around
Haskell's purely functional, recursion-heavy model after doing years
of OOP.

I do think some of the hybrid procedural/OOP languages like C++, Perl,
and Python make this a little harder because OOP is introduced as "a
new, different way of doing things" after you've already gotten used
to a procedural way of doing things. For instance, in C++ classes the
OOP is often treated as a new layer of complexity to learn on top of
what you already know. Whereas in Ruby OOP is just the air you
breathe (although you can ignore it if you wish) from day one.

I don't believe, however, that there's anything inherently more
confusing or complex about sending messages to objects than there is
in passing values to procedures. It's all a matter of background and
what your learned thinking patterns happen to be.
And besides there are many problems
where the OOP machinery just cannot be justified, and is overkill.

That's a little vague and I'm not sure what you're talking about.
Certainly you aren't forced to use any "machinery" you don't want in
Ruby.

Good luck in your Ruby adventures!

--
Avdi

Home: http://avdi.org
Developer Blog: http://avdi.org/devblog/
Twitter: http://twitter.com/avdi
Journal: http://avdi.livejournal.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,281
Latest member
Pedroaciny

Latest Threads

Top