Pry (Ruby REPL): How can I turn automatic echo off - ruby

Since Ruby supports parallel assignments and automatic value return from functions, almost every assignment and method run ends up creating an output when working on REPLs like IRB and Pry.
Normally I prevent this echo effect by putting a semicolon at the end of each line. For instance:
JSON::parse(very_long_json_string);
This normally prevents REPL echo. But when working with very large enumerables even one mistake can generate enough output to make a mess on the screen and put all my useful command history out of memory before I have the reflex to hit the break.
Is there a way to turn this echo effect off by default in Pry? As mentioned in the comments below (#Stefan), the same can be achieved in IRB by setting conf.echo = false.

In IRB there is:
conf.echo = false
In Pry you could replace the print object with an empty proc:
_pry_.config.print = proc {}
You'll have to store the old print object in order to restore it.
In both cases, the result of the last expression is still available via _

Related

Which is correct REPL or command-line?

when i write method missing in Object class i'm getting the output different in each interface.
the code is
class Object
def method_missing(hgh)
puts self
end
end
when i use REPL like irb, i get
when i use the command line, i get no error, any reasons would be helpful, thanks in advance
The tl;dr answer is that both are correct. Just more stuff happen in the REPL.
When you run the code from the command line like:
ruby script.rb
All that happens is that it's evaluated.
Whereas REPLs like IRB read your input, evaluate it and print it in a loop.
In this case evaluating your code literally broke the REPL and resulted in the subsequent print failing.
Now you may be a bit confused by this. "There is a print in both cases, I use puts!". The print I'm referring to here is the result that gets visualised after each evaluation. In this case the method definition result (=> :method_missing).
It might not only be the printing itself. It can be the ton of other under the hood code that the REPL has to execute to keep state like "what code was defined on which line" and so on.
Think of what you just did - you made it so that every object has every method possible to return nil. That is not just for the code you write in the REPL. It's for the code of the REPL itself as well.

Ruby: What Does STDIN.gets mean?

Recently, i was reading legacy ruby script and hopefully to convert some of them to python.
I understand the concept of 'STDIN' and done some google search. However, there is no documentation about this stand alone statement: 'STDIN.gets.' What's the return value? Does it secretly get all the input and store it in some default places, or it is simply a piece of unfinished meaningless legacy code i should delete?
If you run p STDIN.method(:gets) you get #<Method: IO#gets>. This means that the gets method if defined in the IO module. Knowing that, you can then read the official documentation of gets here:
http://ruby-doc.org/core-2.3.0/IO.html#method-i-gets
The gets method does not store its data in some default place, it returns it as the return value of the method. If that return value is being ignored, then the data is lost, but it could still be useful to call STDIN.gets for its side effects: gets will pause the execution of your thread and wait for input from the standard input. This would give the user a chance to review what the Ruby script has already done and terminate it early if the user wants to.
When working with Ruby, irb is your friend. irb is the interactive Ruby shell, or REPL. You can try your statement in irb:
2.3.0 :001 > STDIN.gets
hello
=> "hello\n"
After typing STDIN.gets, the system waited for input. I typed hello and then pressed the Enter key. irb indicated that the return value of the expression was "hello\n".
This shows that:
1) gets returns when, and only when, the user presses Enter or equivalent.
2) gets returns the string that was typed, pasted, etc., including the terminating Enter key.
You'll probably see a lot of calls to chomp on strings returned by gets. This is because the trailing "\n" is rarely wanted. chomp returns a string with the "\n" removed.

How do I use Ruby debugger in Emacs?

I am writing Ruby in Emacs, but my Emacs skills are actually pretty low. What I can do, is open the project, TDD using M-x rinari-test, or play inferior Ruby in the second window using M-x run-ruby. Now I woul like to start using debugger from StdLib. I am able to summon it from irb by saying:
require 'debug'
Upon which I get into a prompt
(rdb:1)
but there my aptitude ends. I don't even know how to step into a file. Typing 'help' brought a screenful, but it didn't help me to finally start debugging my buggy gem. Online, everybody writes about things such as "rdebug" or "ruby-debug" or whatever which I firstly don't want to use and secondly, being a muggle, I am unable to install on my Debian. Please help!!!
You really need to try reading the output of help in the debugger. It explains the commands nicely.
For instance, for practice, try this at the command-line, not inside an editor/IDE:
ruby -rdebug -e 'p 1'
h
Ruby's debugger will output the help summary:
Debugger help v.-0.002b
Commands
b[reak] [file:|class:]<line|method>
b[reak] [class.]<line|method>
set breakpoint to some position
wat[ch] <expression> set watchpoint to some expression
cat[ch] (<exception>|off) set catchpoint to an exception
b[reak] list breakpoints
cat[ch] show catchpoint
del[ete][ nnn] delete some or all breakpoints
disp[lay] <expression> add expression into display expression list
undisp[lay][ nnn] delete one particular or all display expressions
c[ont] run until program ends or hit breakpoint
s[tep][ nnn] step (into methods) one line or till line nnn
n[ext][ nnn] go over one line or till line nnn
w[here] display frames
f[rame] alias for where
l[ist][ (-|nn-mm)] list program, - lists backwards
nn-mm lists given lines
up[ nn] move to higher frame
down[ nn] move to lower frame
fin[ish] return to outer frame
tr[ace] (on|off) set trace mode of current thread
tr[ace] (on|off) all set trace mode of all threads
q[uit] exit from debugger
v[ar] g[lobal] show global variables
v[ar] l[ocal] show local variables
v[ar] i[nstance] <object> show instance variables of object
v[ar] c[onst] <object> show constants of object
m[ethod] i[nstance] <obj> show methods of object
m[ethod] <class|module> show instance methods of class or module
th[read] l[ist] list all threads
th[read] c[ur[rent]] show current thread
th[read] [sw[itch]] <nnn> switch thread context to nnn
th[read] stop <nnn> stop thread nnn
th[read] resume <nnn> resume thread nnn
p expression evaluate expression and print its value
h[elp] print this help
<everything else> evaluate
The important commands to start with are s, n, c and b, and q.
s steps into a method.
n steps over a method.
c number runs (continue) until you reach line number.
b number sets a breakpoint on line number. After setting your breakpoints use c to continue running until that line is executed.
q exits the debugger.
Personally, I use the debugger gem. Others use PRY, which is similar to IRB, but with debugger-like extensions.
Knowing how to use a debugger is a good skill. There are problems you can trace down quickly using a debugger, that will take longer trying to use puts statements, because you can see what a variable contains interactively, or loop conditionally until a variable contains a certain value.

Lua Debugging - Detect when the value of a variable changes

Is it possible to detect when the value of a variable has changed using the lua debug library.
Something like A callback function which would give details like the function in which the value was changed, previous value, etc.
Is such a thing possible?
I read about hooks, but I'm not sure hooks can be set to variables.
If you don't mind using a debugger, then some debuggers allow you to set Watch expressions, which will be triggered when the condition in the expression is true. I'll show how this can be done in MobDebug (it is using lua debug library, but there is no direct way to detect a variable change as far as I know).
Let say we have a script start.lua like the one below and want to detect where foo gets value 2:
print("Start")
local foo = 0
for i = 1, 3 do
local function bar()
print("In bar")
end
foo = i
print("Loop")
bar()
end
print("End")
Download mobdebug.lua and make it available to your scripts (the simplest way is to put it into the folder with your scripts).
Start the server using lua -e "require('mobdebug').listen()" command.
Start the client using lua -e "require('mobdebug').loop()" command.
You will see the prompt in the server window: '>'. Type load start.lua to load the script.
Type step and then step again. You will see "Paused at file start.lua line 3".
Let's see what the value of foo is. Type eval foo and you should see 0.
Now we can set up our watch. Type setw foo == 2. You can specify any Lua expression after setw command; the execution of your script will be stopped when the condition is evaluated as true.
Continue execution of the script using "run" command.
The watch now fires, which will show you the message like: "Paused at file start.lua line 8 (watch expression 1: [foo == 2])". This means that the previous expression changed the value of foo to 2 and the execution is stopped at line 8. You can then inspect your script and the current values (you can use "eval" and "exec" commands to run any Lua code to be evaluated in your script environment) to find what triggered the change.
The benefit of this approach is that you are not limited to monitoring table values and can specify any expression. The main disadvantage is that your script runs under a debugger and the expression is evaluated after each step, which may get really slow.
You can do this to a certain extent in Lua by using metatables and keeping a "proxy" table, and using the __newindex function call to detect attempts to add a variable.
This is covered here in the Programming in Lua book under the section "Tracking Table Accesses":
http://www.lua.org/pil/13.4.4.html
See Also
http://www.gammon.com.au/forum/?id=10887

Why won't ruby-debug allow me to see local variables in my specs?

I am trying to use ruby-debug to debug my specs. When i do this, i am not able to access local variables. Instance variables, however, are fine. Is there a way to make this work with local variables?
Here is an example spec:
require 'spec/autorun'
describe "empty spec" do
it "should be able to be debugged" do
x = 'foo'
#x = 'bar'
debugger
end
end
In the debugger/irb, i can see #x but not x. Detailed output.
Is this just a limitation of using ruby-debug with blocks or is this something I can make work for me?
(In the mean time, i'm using more instance variables than i really should in my specs.)
The example you cite is problematic with ruby-debug. ruby-debug has a limitation that it only stops at statement "line" events and the "debugger" call is the last statement of a block. Therefore in the example you give by the time the debugger stops you are no longer in the "should be able to be debugged" block and x is gone.
One workaround that programmers seem to use a bit is to put some sort of statement after "debugger" but before the end.
In the next incarnation rbdbgr (for YARV Ruby 1.9 with patches), you can stop before returning from the block.

Resources