Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 878469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:46:32+00:00 2026-05-15T11:46:32+00:00

Question: Where does p get it’s value from below and why does it happen?

  • 0

Question: Where does p get it’s value from below and why does it happen?

Consider this irb session:

me@somewhere:~$ irb
irb(main):001:0> a
NameError: undefined local variable or method `a' for main:Object
    from (irb):1
irb(main):002:0> foo
NameError: undefined local variable or method `foo' for main:Object
    from (irb):2
irb(main):003:0> p
=> nil
irb(main):004:0> p.class
=> NilClass
irb(main):005:0>

I never defined p – so why is it nil valued? Neither a nor foo were recognized so what’s special about p? I also didn’t find anything listed under Kernel#p

Context: I’m reading the so-called “28 bytes of ruby joy” and assumed p was a variable, as in: def p.method_missing *_ …

(Don’t worry: I’m not going to actually define method_missing on nil everywhere… just studying some ruby code…)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T11:46:33+00:00Added an answer on May 15, 2026 at 11:46 am

    p is just a method on Kernel which calls inspect on its arguments, producing human-readable representations of those objects. If you give it no arguments, it prints nothing. Regardless of what you pass it, though, it returns nil. See Kernel#p and Object#inspect.

    Power tip: In Ruby 1.9, when you have a method and you don’t know where it came from, use the method method:

    ruby-1.9.1-p378 > method(:p)
     => #<Method: Object(Kernel)#p>
    

    Putting it together one step at a time, we read this as:

     p                            # We found a method called p.
     #p                           # It's an instance method.
     Object ... #p                # It's available on Object.
     Object(Kernel)#p             # It came from the Kernel module.
    

    Update: The OP provided some context from this article, where the author claims that your life will be easier if you add a method_missing to nil, by doing the following:

    def p.method_missing*_;p;end
    

    This somewhat obfuscated code should be read as:

    • Define a new method (def), called method_missing. This overrides the default method_missing handler on Object, which simply raises a NoMethodError when it encounters a method it doesn’t understand.
    • This method will live on something called p.
    • It accepts any number of arguments (*) and stores them in a variable called _.
    • The result of these arguments is something called p.

    The second bullet is the tricky part here. def p.method_missing means one of two things, depending on context:

    • A previously defined object called p which is in scope here.
    • A method called p which is in scope, and which is passed no arguments.

    With def p.method_missing, we mean, “this method is being defined on the object which is the result of calling p with no arguments”. In this case, that is NilClass; if you call p with no arguments, you get nil. So this is just a short, hacky way to define a method on NilClass.

    Note: I definitely recommend against defining a method_missing on nil. This is a silly and dangerous tactic to use for the sake of saving a few lines of code, because it changes the behavior of nil. Don’t do it!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 508k
  • Answers 508k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you cannot find how to take out the xammp… May 16, 2026 at 4:26 pm
  • Editorial Team
    Editorial Team added an answer I can't say for sure why it's not working since… May 16, 2026 at 4:26 pm
  • Editorial Team
    Editorial Team added an answer It's definitely intuitive, as the current directory of the running… May 16, 2026 at 4:26 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

How does one get the current user in a portable way? This seems like
Does anyone else get this problem or know a solution / workaround I could
does anyone know if you can get individual field comments from mysql using Zend
What does mean? I get this error when trying to iterate through a file
For some reason, after submitting a string like this Jack’s Spindle from a text
Does get(key) require the entity key and get_by_key_name(key_names) require the key_name?
Does MXML get compiled down to as3 and then converted to flash bytecode? Also,
Does file_get_contents maintain line breaks? I thought it did but I have tried this:
Does libpcap get a copy of the packet or the actual packet? By copy,
Does viewDidLoad get called when you're popping back to a UIView?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.