Debugging: What is binding in Ruby?

You might have thought always whenever you wrote code binding.pry in your application to debug your application.

People often say “do binding to see whats going on”. Actually you are not doing binding but prying into the binding. So, its good to say “Please pry into that binding”.

But, what is binding in the first place? Continue reading

JavaScript : jQuery : Best way to add Event Handlers makes debuging easy

I work on Rails project with 20+ javascript files. I even have implement the Javascript/script response using the js.erb template. Also, I am using jQuery instead of the vanillaJs to code easily. These days I figured out that in developer mode in browsers its impossible to find the actual event handler function by pointing at any node element. It was awesomely easy when there was no jQuery or any other library. Continue reading

Ruby On Rails : Unknown issue : Function returns unexpected things when I debug/pry

Sometimes when we debug in development or try to log what is happening in production mode, we encounter with some mysterious issues of unexpected value returned by the function/methods. Let us see the scenarios

Scenario 1:

def destroy
  item = get_item
  if item.delete
    flash.now[:notice] = 'Successfully deleted'
  end
end
private
def get_item
  Item.find(params[:id])
end

Scenario 2 :

def destroy
  item = get_item
  if item.delete
    flash.now[:notice] = 'Successfully deleted'
  end
end
private
def get_item
  Item.find(params[:id])
  # To pry the value, used binding.pry or logger.into 'inside get_item'
  binding.pry
  # logger.into 'inside get_item'
end

Findings:  In scenario 1, every thing is alright however, scenario 2 is error prone, error such as ‘NoMethodError: Undefined method `delete for “item”‘ is expected to arise.

This is because, whenever ruby interpreter executes the method ‘get_item‘ it finds ‘binding.pry‘ which initiates new ‘irb‘ session and on ‘exit‘ the ‘binding.pry‘ returns ‘false‘. This false is now returned by the method ‘get_item‘. In case of ‘logger.info or logger.error‘ the method ‘get_item‘ returns what the ‘logger.error‘ returns.

Rails application:: Assets not precompiling

In rails, you may some time have problem of  JS or CSS not being up-to-date. This problem may occur due various reasons:

  • It may occur due to caching by browser: To solve this press CTRL + SHIFT + R button in browser
  • It may occur due to assets caching by Rails server: To solve this type following command in terminal.      [You must be inside the rails app directory in terminal]
rake tmp:cache:clear
rake assets:precompile

# To ensure it works properly, please restart the server once