How do I inspect an object in Laravel? - laravel

I've used the pry gem in the past to inspect objects in Ruby on Rails. Is there a way to do this in Laravel?

As PRY is more like an attempt for a REPL console..
you should be able use 'php artisan tinker' as REPl to execute and inspect objects.
Not sure if it's installed by default:
https://github.com/laravel/tinker
However inspecting / debuging on itself, you should consider xdebug, with IntelliJ IDE / PHPStorm or any other IDE supporting XDebug.
It gives you many advantages and make debugging and work more effective

Related

How to see the source code for an "update!' method

I have a web site that is killed due to a memory overflow. It is triggered during a PUT request coming from a users web browser. Unfortunately, the logs are not helpful in this case. I have traced the issue down to this method definition:
# app/controllers/registrations/profiles_controller.rb
def update
update! do |success, failure|
success.html { redirect_to edit_registration_diagnosis_path }
failure.html do
build_diagnosis
render 'edit'
end
end
end
I want to see the source code for this update! method. How do I ask ruby or rails or bash/grep to show me this source code?
I tried:
git grep 'def update!' # no results
My env:
$ rails --version
Rails 3.2.22.5
$ ruby --version
ruby 1.9.3p551
The libraries are not in the same directory as your Rails app, they'll be wherever your Ruby versions are, which differs depending on which version manager you used to install it.
The Rails docs are online at http://api.rubyonrails.org/, or you could use a gem like pry-byebug to step into the method during execution. As Ruby is object oriented and uses an inheritance chain to find an object that responds to a given message, this is the best way to really know which method is being called at any given point in the application's execution.
Add gem 'pry-byebug' to your gemfile, bundle install and then insert binding.pry at the top of your update method. Once execution pauses you can easily step into the method.
You can use byebug gem to see what is happening at each step in your method.
As others have said, the code in your app is not the full set of code that is running. So grep won't work here. You may also run into the issue where the same method is defined multiple times, and grep won't help there, either.
The best solution I've found is using pry. It looks like this is a Rails project, so you can get results most easily by adding the following to your Gemfile:
# Gemfile
gem "pry-rails"
gem "pry-doc"
At this point, the world is your oyster.
The easiest way to start learning how to use this is to add a binding.pry in your code execution where you want to explore. Then run the code in test or development environments, and your server will stop and give you a console at that line. Then you just show-source update! and you'll see where the method is defined.
So step 1, use pry and explore its many plugin libraries.
Step 2 is to try out solargraph in your text editor. It's not as powerful as pry, but it can help you jump to method definitions within your project easily. https://solargraph.org
Step 3 check out the premium text editor RubyMine, as it has support for this sort of thing and much more, though it's not free. https://www.jetbrains.com/help/ruby/getting-started.html

Errors running Laravel Dusk

I just discovered Laravel Dusk, and I was excited to use it in my project. However, it can't seem to get it to work. I followed the Laravel Documentation to install Dusk. When I run php artisan dusk in the terminal, I get
[Symfony\Component\Process\Exception\RuntimeException]
TTY mode is not supported on Windows platform.
I found this (https://github.com/laravel/dusk/pull/13/files) which is supposed to fix it I think, but I still get the error.
Also, I attempted to run the Dusk tests through PhpStorm following the accepted answer here (https://laracasts.com/discuss/channels/testing/dusk-via-phpstorm), but I get a Class config does not exist error. Many people said that this caused by a space in the .env file, but that is not my issue.
Can anyone help me figure out what is going on with Laravel Dusk in my setup? My Laravel project is 5.4 and I'm using Laravel Dusk 1.0 and PhpStorm is 2017.2.1
You can install chromedriver into windows on your own, and try not to use the version that is shipped with laravel dusk. Remember that, your chromedriver version needs to match your currently installed Chrome version, and when Chrome gets updated, then you need to update/reinstall chromedriver manually again. Hopefully it does not happen too often, and when there are updates, you benefit from using latest versions, which I find really nice :)
Download it here.
After installing chromedriver, you need to start it from command line (just issue command in your download/install directory chromedriver) or add it as service (which I do not prefer, cause I want to have control of which service is running on 9515 port).
Finally you need to comment out // static::startChromeDriver(); in your DuskTestCase.php, which tries to start laravel dusk included chromedriver, cause in this case, you are already running one.
Run dusk tests again and all should be working fine.
I don't know what your system setup is, but I am using this workaround on windows 10 with Laravel 5.5, dusk 2.0, which still causes problems, when chrome gets updated.
BTW. Just to mention. This also works with CI workflows using docker. You can read more about it here

How to debug the code to be tested

When I run an rspec test, I want to get details of the variables in the code that is tested. I tried to use rubymine, but it is complicated to configure. Sublime text 2 is a ruby friendly editor, so I wish someone can recommend some plugins to me.
Unfortunately, I don't know of any Sublime plugins that will do this for you, but...
Check out the pry gem. This will allow you to peek into your code, view instance variables, modify settings, etc. within the middle of an Rspec test.
There is no similar package for Sublime. What you are looking for is a debugger (in fact, RubuyMine integrates the Ruby debugger).
The most common Ruby debugger is the debugger gem. Simply install it, then place a debugger statement where you want to debug the execution and you'll be able to jump in the middle of the running spec execution.

How to traverse through Source code only using debugger - Rails 3.2.13

In my Rails app, using Ruby 2.0.0-p0 and Rails 3.2.13, I also use the gem debugger for debugging purpose. If I have to debug an area in my source code, for e.g. a controller method, I will put debugger inbetween and when I execute the action, it will go to the source code and then travserse through the framework code a lot and that really mess-up the normal debugging. I can see many commands through help, but don't know how to handle with that.
I just want to traverse through the source code only..
Please help me to have a solution. Thanks :)-
Use byebug instead. It was created to overcome debugger's problems with ruby-2.0.0, like the one that makes it "step" when you use command "next", unexpectedly leading you to framework sources.
Disclaimer: I'm byebug's author

How to incorporate Interactive Ruby into my development process?

I am trying to find a better way to integrate IRB with my normal ruby devleopment. Currently I rarely use IRB with my code. I only use it to verify syntax or to try something small.
I know I can load my own code into ruby as a
require 'mycode'
but this usually doesn't mesh with my programming style. Sometimes the variables I want to examine are out of scope or inside of a loop. Is there an easy way to fire up my script and freeze at a certain point inside of IRB? I guess I'm looking for an easier way to debug my ruby code without breaking my F5(compile) key.
Maybe a more experienced ruby developer can share with me a more streamlined method of development.
Install the ruby-debug gem. Of course, require it inside your app (only in development/test mode). Now you can write 'debugger' where you want to stop execution.
Once your app stop at your breakpoint, you can type 'help' to know about all commands. One of them is 'irb'. It starts an IRB session in which you have access to all methods in your current context.
I personally mostly use p (print), eval, v i (instance vars) and v l (local vars). Of course, n for next and c for continue.
The command to step out of a given block/method never worked for me though. I never investigated why :-)
I don't tend to use irb directly that frequently, as I tend to be inside rails and so use script/console a bunch, but I do like using the ruby debugger (Ruby Debug gem). It lets you set a breakpoint basically and then step through your code line by line.
Here's a screencast about it that I haven't actually watched, but a quick search pulled it up, and it could be useful:
http://brian.maybeyoureinsane.net/blog/2007/05/07/ruby-debug-basics-screencast/
For Ruby development in Eclipse: there's a much improved version of RDT (ruby development tools) available now. To install it directly in Eclipse, click Help > Software Updates > Find and Install > Search for new features radio button > next > new remote Site > Name = Ruby and URL = http://update.aptana.com/update/rdt/3.2/
Another Ruby plugin is the shiny new Eclipse DLTK (dynamic languages toolkit).
DLTK stable release 1.0.M5 just came out a few days ago.
Here are some useful installation tips.
I just use rdebug to debug any of my ruby or RoR code.
If you're willing to use an IDE for debugging, I know Eclipse (via the Ruby Development Tools) has a relatively straightforward interface. If you're doing rails then there's a specific build of eclipse called RadRails which may also help (though I haven't used it for debugging)

Resources