What is "p"?

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?

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?

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

2008/7/24 matu [email protected]:

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

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

def ola

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

Farrel L. 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?

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

James G. wrote:

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

Object.private_methods.include? “p”
=> true

Doh! :slight_smile:
May I ask why?

To make sure you can’t do this:

some_rand_obj.p …

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

James Edward G. II

James G. wrote:

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

Object.private_methods.include? “p”
=> true

Doh! :slight_smile:
May I ask why?

matu wrote:

Doh! :slight_smile:
May I ask why?

All right… I am starting to look like a 3y old why, why, why, why…:slight_smile:
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?

James G. 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 :slight_smile:

James G. wrote:

What is different is how you can define them.  You can use “…” or ‘…’
to construct String objects with different rules about how the content
will be treated.

clear as mud :slight_smile: thanks!

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

“”.class
=> String

‘’.class
=> String

They are both String classes, but as we all know, they behave
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 “…” or ‘…’
to construct String objects with different rules about how the content
will be treated.

James Edward G. II

matu wrote:

James G. wrote:

What is different is how you can define them. You can use “…” or ‘…’
to construct String objects with different rules about how the content
will be treated.

clear as mud :slight_smile: thanks!

like so…

irb(main):001:0> h=‘huh?’
=> “huh?”
irb(main):002:0> p ‘#{h}’
“#{h}”
=> nil
irb(main):003:0> p “#{h}”
“huh?”
=> nil

On Jul 24, 1:15 pm, matu [email protected] 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?