[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Ruby introspection????

r

1/10/2009 2:50:00 AM

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

Avdi Grimm

1/10/2009 3:14:00 AM

0

On Fri, Jan 9, 2009 at 9:49 PM, r <rt8396@gmail.com> wrote:
> 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://rub...

--
Avdi

Home: http:...
Developer Blog: http:.../devblog/
Twitter: http://twitte...
Journal: http://avdi.livej...

Robert Klemme

1/10/2009 10:05:00 AM

0

On 10.01.2009 04:14, Avdi Grimm wrote:
> On Fri, Jan 9, 2009 at 9:49 PM, r <rt8396@gmail.com> wrote:
>> 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

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

Radoslaw Bulat

1/10/2009 10:41:00 AM

0

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

--=20
Pozdrawiam

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

Michael Fellinger

1/10/2009 3:45:00 PM

0

2009/1/10 Rados=C5=82aw Bu=C5=82at <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).

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

^ manveru

r

1/10/2009 3:46:00 PM

0

On Jan 10, 4:41 am, Radoslaw Bulat <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:


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.

Avdi Grimm

1/13/2009 6:01:00 PM

0

2009/1/10 r <rt8396@gmail.com>:
> 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:...
Developer Blog: http:.../devblog/
Twitter: http://twitte...
Journal: http://avdi.livej...