Skip to main content

Posts

Showing posts from July, 2020

Getting started with Docker

Docker, in the beginning, can be overwhelming. Tutorials often focus on creating a complex interaction between Dockerfiles, docker-compose, entrypoint scripts and networking. It can take hours to bring up a simple Rails application in Docker and I found that put me off the first few times I tried to play with it. I think a rapid feedback loop is essential for playing with a piece of technology. If you've never used Docker before, then this is the perfect post for you. I'll start you off on your docker journey and with a few simple commands, you'll be in a Docker container, running ruby interactively. You'll need to install Docker. On a Mac, I prefer to install Docker Desktop through homebrew: brew cask install docker If you're running Linux or Windows, read the official docs for install instructions. On your Mac, you should now have a Docker icon in your menu bar. Click on it and make sure it says "Docker desktop is running". Now open a terminal and ty

Fun with Ruby - to_s

irb> 1 => 1 irb> 1.to_s => "1" irb> 1.to_s * 2 => "11" irb> "1" *2 => "11" irb> 1.to_s *2 => "1" # huh? #let's try that again with 2.... irb> 2.to_s => "2" irb> 2.to_s * 2 => "22" # good irb> "2" *2 => "22" irb> 2.to_s *2 => "10" # wtf? Sometimes a space makes all the difference. Ruby's #to_s on an Integer takes an optional parameter which specifies the base you are working in. In this case, passing in *2 has set our base to binary.

Development tips - don't use request as a variable name

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: 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 reasona