I have some code (a Rails app) that generates output to console.
I'd like to use byebug to get the location of whatever is generating that output.
Is there a way to do that?
Could you specify a little more? What do you mean by having "the location of whatever is generating that output"? Do you mean the trace? If yes, buybug has a backtrace (you can use where also) command. Have you look into this?
EDIT:
You could use pry with byebug (with the pry-byebug) and accomplish what you want.
Use pry gem for debugging code
Gemfile
gem 'pry'
add binding.pry anywhere to debug code
Related
I'm wondering if it's possible to start a Byebug session giving a starting point from a Rails console. I know I can insert a byebug statement wherever I want and start debugging, but I'd like to do something like this:
Byebug.start do
# entry point
User.find(12).problematic_method
end
Thanks.
I opened the class and override the problematic_method inside the Rails console and added the byebug statement where I wanted it. This way I don't have to change the running production code (I forgot to mention above I want to debug in production).
This workaround will be enough for my purposes. The only problem is that you don't have the debug code listing available for that method, but its fine.
That is not possible. What you can do, is write your code inside a .rb file and debug that file/script using byebug.
is a very beginner question but even tho i cannot find any helpful documentation online.
I'm working on Discourse application which is Ruby on Rails + Ember based.
I need to print out in my terminal (not in my view!) the value of a variable.
I try:
debugger
put varName
logger.debug varName
but none of them print anything in my console.
What i need to do is stop my console from running and print a value.
Like debugger; would do in javascript.
How can i do this simple task?
1) not sure if this change anything but the .rb file i try to debug is a "services".
2) yes, i read about pry, i installed it and add to my file:
require 'pry'
binding.pry
But i don't see anything happening in the console.
I've tried pry and remote-pry, but no luck. I'm familiar with logging, but I want to be able to step thru my code and look at variables.
Does anyone know of anything I can use to debug Sidekiq?
Workers are designed to be trivial to run. Put pry in your worker code and run it in the rails console.
> MyWorker.new.perform(some_args)
The best thing I've come up with is this gem gem 'pry-remote' it works great and stops all processes from running. And it works like pry just put in binding.remote_pry and you've got a stopping point.
You can use byebug but you have to require it inside the class definition of the job.
For instance,
class SomeJob < ActiveJob::Base
require 'byebug'
def perform(*args)
byebug
end
end
then in your rails console run
SomeJob.perform_now(*args)
This will cause a breakpoint to appear where ever you have byebug called with your typical byebug prompt inside your rails console.
I'm trying to test if my values are existing... Not sure how to do this, I'm just starting to learn Ruby on Rails. Hopefully someone can point me to the right direction?
Lets say I have this block of codes:
#lv = {'apple' => ['red', 'round'], 'iPhone' => ['device', 'phone']}
if params[:var]
#lv.each do |key, tags|
if params[:var] == key
lvtags = #lv[params[key]]
lvtags.each do |tag|
#tags = client.tag_recent_media(tag)
end
end
end
end
I'm trying to see if the loop through the if params[:var] == key works. I'd like to somehow output like an alert() type thing, to test if key has a value? Is there something like alert(key)? where it'll show if key has something? Or if I can test if lvtags has a value that its supposed to pop out?
For instance, if ?var=0
Then I'd like to test alert(lvtags), and thus this is supposed to pop out apple or something, some value associated to lvtags = #lv[params[key]]. How do we normally test something like this in rails?
Thanks
You can do a puts key. Puts converts its first param to string and outputs it in the stdout (your console).
It might be easier for you to use a debugger, though. You can open a console debugger (think gdb) with
require 'debugger'; debugger
Be sure to install the debugger gem first. Put the following in your Gemfile
gem 'debugger', group: [:development, :test]
Edit: added a link to the debugger gem's github page.
You can just log it in your console.
Rails.logger.info key.inspect # or whichever's value you want to check
It's also helpful when checking objects
How do I debug the rails gems? I've just found the beauty of ruby-debug and am real excited, but I want to step through the routing code in action_controller, but the debugger isn't set up at this time, it seems like it's just skipping over my "debugger" calls in action_controller/routing/route_set.rb for example.
What am I missing?
I just tested this with Rails 2.3.4. I added a 'debugger' line to the call method in vendor/rails/actionpack/lib/action_controller/routing/route_set.rb, ran 'rdebug script/server', browsed to a page, and it stopped at the correct line.
You can also use a class/method breakpoint; you'll need to step through the first few lines of the app until you're past the require 'action_controller' line, and then enter: b ActionController::Routing::RouteSet.call.
Or you can try setting a breakpoint based on the file name and line number.