Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

226
Views
Why does calling pp pp not loop?

If I assign a value to the variable pp and then call pp pp in the console why doesn't pretty print loop?

$ irb

pp = "hello world"
=> "hello world" 
pp pp
"hello world"
=> "hello world" 

According to this explanation I think it should

https://blog.brycekerley.net/2014/08/27/Working-with-pp-the-Ruby-Pretty-Printer.html

Edit: I didn't mean that it must loop, I just wanted to know the method call order that happened that prevented it from looping.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Ruby is an interpreted language, meaning that, instead of compiling, it executes the code as written, interpreting the code line by line, word by word (or rather token by token).

When ruby interpreter encounters a line pp pp, it needs to resolve two tokens. First one is clearly a method as it takes an argument, the other one can be either a method OR local variable (there are no brackets nor arguments).

So, during execution, ruby will start resolution from the second token. When it is not clear whether token denotes variable or method, ruby always searches for variable first. In this case, the variable is there as you assigned a value to pp creating a local variable (let's say by pp = 1).

Then ruby has still to process the second token, but now it looks like pp(1) (because first token has been resolved already). In this case this is, again, clearly a method so ruby just send a pp message to self (which in this context is (main) object). Method pp is defined on Kernel, so each object - including (main) can access it.

The key point here is to understand that you have not overridden method pp - that method is still there. The only thing that happened is different resolution of pp token. Even with pp hidden (or shadowed) by local variable, you can still invoke it either by using explicit self.pp or by making sure that it looks like a method pp()

over 4 years ago · Santiago Trujillo Report

0

I'm going far on a limb here (for fun mostly) and guessing you do something like this

>> pp "this is a test"
"this is a test"
=> "this is a test"
>> pp = pp
=> nil
>> pp pp
nil
=> nil
>> pp "this is another test"
"this is another test"
=> "this is another test"

So here's what happens

pp = pp

You've created a local variable pp which got assigned a value of whatever pp method returned (nil in this case)

>> pp pp

here you call pp method and passing it pp variable, which holds nil

>> pp "this is another test"
"this is another test"
=> "this is another test"

you still can call pp method as usually.

Not sure why would you expect it to loop. Expressions are evaluated (right to left in general), so they will evaluate eventually. No infinite loops should generally happen. Take this example:

x = y = z = x

It will first evaluate right-most x (which has not been defined, so defaults to nil, so next step conceptually will be:

x = y = z = nil

assigns nil to z, and result of this evaluation is also nil

x = y = nil

assigns nil to y and so on...

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!