Ruby introspection?

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 :slight_smile:

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??

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??

路路路

On Fri, Jan 9, 2009 at 9:49 PM, r <rt8396@gmail.com> wrote:

--
Avdi

Home: http://avdi.org
Developer Blog: Avdi Grimm, Code Cleric
Twitter: http://twitter.com/avdi
Journal: http://avdi.livejournal.com

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

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>

5.) id(obj)

obj.id

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

Kind regards

  robert

路路路

On 10.01.2009 04:14, Avdi Grimm wrote:

On Fri, Jan 9, 2009 at 9:49 PM, r <rt8396@gmail.com> wrote:

--
remember.guy do |as, often| as.you_can - without end

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).

路路路

On Sat, Jan 10, 2009 at 11:09 AM, Robert Klemme <shortcutter@googlemail.com> wrote:

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

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

--
Pozdrawiam

Rados艂aw Bu艂at
http://radarek.jogger.pl - m贸j blog

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

路路路

2009/1/10 Rados艂aw Bu艂at <radek.bulat@gmail.com>:

On Sat, Jan 10, 2009 at 11:09 AM, Robert Klemme > <shortcutter@googlemail.com> wrote:

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

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

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).

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.

路路路

On Jan 10, 4:41 am, Rados艂aw Bu艂at <radek.bu...@gmail.com> wrote:

On Sat, Jan 10, 2009 at 11:09 AM, Robert Klemme > > <shortcut...@googlemail.com> wrote:
>>> 2.) help(obj) -> <no brainer>

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

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:

Hi,

路路路

In message "Re: Ruby introspection???" on Sun, 11 Jan 2009 00:49:32 +0900, r <rt8396@gmail.com> writes:

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.

ri is your friend. We'd rather choose separated document command
rather than consuming interpreter memory. Integrating ri into irb
might be a good idea though.

              matz.

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!

路路路

2009/1/10 r <rt8396@gmail.com>:

--
Avdi

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

WOW, i can't believe you popped in on this thread. I feel as if
royalty has visited me. Thank you! This shows you really care.

路路路

On Jan 10, 10:00 am, Yukihiro Matsumoto <m...@ruby-lang.org> wrote:

Hi,

In message "Re: Ruby introspection???" > on Sun, 11 Jan 2009 00:49:32 +0900, r <rt8...@gmail.com> writes:

>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.

ri is your friend. We'd rather choose separated document command
rather than consuming interpreter memory. Integrating ri into irb
might be a good idea though.

                                                    matz\.

Actually this seems to be what the "help" method does that Michael mentioned. When I type "help String" in IRB I get the same result as from "ri String" on command line, i.e. the doc in a pager. For methods you have to do "help 'String#length'".

Thanks, Michael!

Kind regards

  robert

路路路

On 10.01.2009 17:00, Yukihiro Matsumoto wrote:

Hi,

In message "Re: Ruby introspection???" > on Sun, 11 Jan 2009 00:49:32 +0900, r <rt8396@gmail.com> writes:

>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.

ri is your friend. We'd rather choose separated document command
rather than consuming interpreter memory. Integrating ri into irb
might be a good idea though.

--
remember.guy do |as, often| as.you_can - without end