Many web development frameworks like Ruby on Rails or Padrino or Sinatra make use of some magic variables. It's always a good idea to become at least partially familiar with these in case you fall into the trap of over-writing one of these, sort of akin to the way you can, in Javascript, carelessly declare a variable called "console" and completely destroy your ability to debug your javascript in chrome.
This sort of problem can arise in Ruby on Rails when you are hacking away in a controller. Perhaps you've got a gem in your project that calls an api on the web - Let's call it the rubyflewtoo api that issues a random number when you call the "get" method. You might be tempted to write some code like this:
And the gem magically gives you back a random number between your lower and upper bounds. The code makes sense and the variables look reasonably named - you are, after all, issuing a request for a random number over http. The problem with this, is the subtle bug that you've introduced if later in your controller you want to grab something like the ip address of the user for some location awareness, you've over written the default Rails request object.
This sort of problem can arise in Ruby on Rails when you are hacking away in a controller. Perhaps you've got a gem in your project that calls an api on the web - Let's call it the rubyflewtoo api that issues a random number when you call the "get" method. You might be tempted to write some code like this:
request = rubyflewtoo.new
@random_number = request.get bounds: {'lower':10, 'upper':20}
And the gem magically gives you back a random number between your lower and upper bounds. The code makes sense and the variables look reasonably named - you are, after all, issuing a request for a random number over http. The problem with this, is the subtle bug that you've introduced if later in your controller you want to grab something like the ip address of the user for some location awareness, you've over written the default Rails request object.
Comments
Post a Comment