[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

what is "p"?

matu

7/24/2008 7:12:00 PM

a really really stupid question:

>> p "hello"
"hello"
=> nil
>>

is p a method of object?

>> self.class
=> Object
>> self.methods
=>
["inspect", "popws", "quit", "irb_fg", "clone", "method", "public_methods", "public", "irb_cwws", "instance_variable_defined?", "irb_help", "irb_change_binding", "equal?", "freeze", "pushb", "pretty_print_instance_variables", "conf", "irb_print_working_binding", "workspaces", "methods", "respond_to?", "irb_pop_binding", "irb_chws", "irb_pushws", "dup", "pretty_inspect", "irb_pwws", "irb", "irb_require", "instance_variables", "irb_cb", "kill", "__id__", "irb_context", "irb_pop_workspace", "eql?", "object_id", "irb_cwb", "irb_jobs", "irb_bindings", "id", "irb_current_working_workspace", "irb_popb", "singleton_methods", "send", "irb_cws", "fg", "taint", "pushws", "frozen?", "instance_variable_get", "private", "cwws", "__send__", "cb", "help", "instance_of?", "to_a", "irb_pwb", "type", "bindings", "protected_methods", "instance_eval", "popb", "==", "display", "irb_kill", "chws", "===", "install_alias_method", "irb_push_binding", "instance_variable_set", "pwws", "irb_source", "kind_of?", "extend", "irb_workspaces", "pretty_print_cycle", "irb_quit", "irb_popws", "pretty_print_inspect", "to_s", "irb_change_workspace", "irb_exit", "jobs", "hash", "irb_push_workspace", "class", "include", "irb_print_working_workspace", "irb_load", "tainted?", "=~", "private_methods", "cws", "source", "nil?", "irb_pushb", "untaint", "exit", "irb_current_working_binding", "context", "pretty_print", "is_a?"]

no. looks like it's not. so where does it come from? is it a ruby method?
13 Answers

matu

7/24/2008 7:15:00 PM

0

matu wrote:

> no. looks like it's not. so where does it come from? is it a ruby method?

oh. i found out: it comes from Kernel. put this raises another question: are
all methods of Kernel available in Object? is Kernel always mixed in or
something like that?

David A. Black

7/24/2008 7:19:00 PM

0

Hi --

On Fri, 25 Jul 2008, matu wrote:

> matu wrote:
>
>> no. looks like it's not. so where does it come from? is it a ruby method?
>
> oh. i found out: it comes from Kernel. put this raises another question: are
> all methods of Kernel available in Object? is Kernel always mixed in or
> something like that?

Yes.


David

--
Rails training from David A. Black and Ruby Power and Light:
Intro to Ruby on Rails July 21-24 Edison, NJ
* Advancing With Rails August 18-21 Edison, NJ
* Co-taught by D.A. Black and Erik Kastner
See http://www.r... for details and updates!

Farrel Lifson

7/24/2008 7:23:00 PM

0

2008/7/24 matu <m@t.u>:
> oh. i found out: it comes from Kernel. put this raises another question: are
> all methods of Kernel available in Object? is Kernel always mixed in or
> something like that?

That's correct. The Kernel module is mixed into Object (and hence is
available in every object as every object is a descendant of Object).

Farrel
--
Aimred - Ruby Development and Consulting
http://www....

James Gray

7/24/2008 7:44:00 PM

0

On Jul 24, 2008, at 2:39 PM, matu wrote:

> Farrel Lifson wrote:
>
>> That's correct. The Kernel module is mixed into Object (and hence is
>> available in every object as every object is a descendant of Object).
>>
>
> Ok. Thank you both. But...
>
> hi.rb:
> module Hi
> def ola
> p 'ola'
> end
> end
>
>>> require 'hi'
> => true
>>> include Hi
> => Object
>>> Object.methods.include? 'ola'
> => true
>>> ola
> "ola"
> => nil
>>> Object.methods.include? 'p'
> => false
>
> WTF? Why does Object.methods not include p? Kernel is mixed in like
> my Hi
> module, right? So why does Object.methods include ola, but not p?

It does. You just need to know that it's a private method:

>> Object.private_methods.include? "p"
=> true

James Edward Gray II

matu

7/24/2008 7:45:00 PM

0

Farrel Lifson wrote:

> That's correct. The Kernel module is mixed into Object (and hence is
> available in every object as every object is a descendant of Object).
>

Ok. Thank you both. But...

hi.rb:
module Hi
def ola
p 'ola'
end
end

>> require 'hi'
=> true
>> include Hi
=> Object
>> Object.methods.include? 'ola'
=> true
>> ola
"ola"
=> nil
>> Object.methods.include? 'p'
=> false

WTF? Why does Object.methods not include p? Kernel is mixed in like my Hi
module, right? So why does Object.methods include ola, but not p?

matu

7/24/2008 7:56:00 PM

0

James Gray wrote:

> It does.  You just need to know that it's a private method:
>
> >> Object.private_methods.include? "p"
> => true

Doh! :)
May I ask why?

James Gray

7/24/2008 8:02:00 PM

0

On Jul 24, 2008, at 2:54 PM, matu wrote:

> James Gray wrote:
>
>> It does. You just need to know that it's a private method:
>>
>>>> Object.private_methods.include? "p"
>> =3D> true
>
> Doh! :)
> May I ask why?

To make sure you can't do this:

some_rand_obj.p =85

It's private, so Ruby knows that is no good.

James Edward Gray II


matu

7/24/2008 8:13:00 PM

0

matu wrote:

> Doh! :)
> May I ask why?

All right... I am starting to look like a 3y old why, why, why, why...:)
But today I have some time, and I am playing around, and there is something
else that surprises me a bit:

>> "".class
=> String
>> ''.class
=> String
>>

They are both String classes, but as we all know, they behave differently.
How?

matu

7/24/2008 8:15:00 PM

0

James Gray wrote:

> To make sure you can't do this:
>
> some_rand_obj.p â?¦
>
> It's private, so Ruby knows that is no good.

All right, that sounds like a really good reason :)

James Gray

7/24/2008 8:15:00 PM

0

On Jul 24, 2008, at 3:09 PM, matu wrote:

> matu wrote:
>
>> Doh! :)
>> May I ask why?
>
> All right... I am starting to look like a 3y old why, why, why, =20
> why...:)
> But today I have some time, and I am playing around, and there is =20
> something
> else that surprises me a bit:
>
>>> "".class
> =3D> String
>>> ''.class
> =3D> String
>>>
>
> They are both String classes, but as we all know, they behave =20
> differently.
> How?

They are both String objects and there is no difference between them.

What is different is how you can define them. You can use "=85" or '=85' =
=20
to construct String objects with different rules about how the content =20=

will be treated.

James Edward Gray II