Categories
Beginners code ruby ruby on rails rubygem unit tests

Read Minitest To Learn Ruby

I continue my quest to master Ruby. Lately I’ve been reading the source code for Minitest. Minitest is a Ruby gem to help developers write unit tests. In addition, you can also use Minitest to write specs and do your development BDD style. The gem also has support for mocking and benchmarking.

Let’s concentrate our attention on the assertions.rb file located at https://github.com/seattlerb/minitest/blob/master/lib/minitest/assertions.rb. Inside that file, we can see that the assert method takes 2 parameters: test and msg. Below is the actual Ruby code.

##
#  Fails unless +test+ is truthy

def assert test, msg = nil
  self.assertions += 1
  unless test then
    msg ||= "Failed assertion, no message given."
    msg = msg.call if Proc === msg
    raise Minitest::Assertion, msg
  end
  true
end

The first line inside the assert method, self.assertions += 1, is increasing the assertions variable by 1. This value is display after your unit tests are run. Something like 10 assertions, 5 pass, 5 failed. Next we see the unless keyword followed by test and a block. Having the comment above this method, really helps us understand what unless is. The unless block will be executed if test is false. Inside that block, we set a default message to msg if msg is nil or false. The next line checks if the msg passed to our method is a Proc object. If msg is a Proc object, msg will be assigned the value returned from invoking the block. To learn more about the Proc class, go to the official Ruby docs. Then we raise an exception by calling Minitest::Assertion and passing the msg variable.

After executing the unless block, we return true.

I believe Minitest is an excellent resource to learn Ruby. It’s a small code base compared to other projects.

Categories
ruby ruby on rails rubygem

Why Ruby’s File.executable? method behaves different between Windows and Linux

Ruby File.Executable

I was working with the RSpec team about some issues with their Windows’ continuous integration build hosted at Appveyor. One of the unit tests was failing on Windows but it was working correctly on Linux. After running only the failing test, I was able to identify the issue with the unit test that was using Ruby’s File.executable? method.

The RSpec team uses Travis CI to run and build their software against Linux operating systems. To run and build their software against Windows, they use Appveyor.

It was strange to me that File.executable? method was working fine on linux but it was failing on Windows. The first thing I did was to read the official Ruby documentation on the File class. After reading the documentation a few times, one word received my attention, “executable”. This is what the documentation says about File.executable?: “Returns true if the named file is executable by the effective user id of this process.”

In the same document, it says “On non-Posix operating systems, there may be only the ability to make a file read-only or read-write.” This means that the executable permission is not available in Windows. It is only available on *nix operating systems.
After reading the official File documentation, I wanted to see if other projects have experienced the same issue. I was able to find a bug report for Puppet Labs titled “Windows File.executable? now returns false on ruby 1.9.” The team decided to close this bug and made minor changes to their unit tests to be OS specific.

We decided to do the same with the RSpec team. Here is the diff view to see the changes.

Categories
open source ruby ruby on rails rubygem

RubyGem of the week: Rails

RubyonRails

RubyGems is a package manager for Ruby software. It allows you to create and distribute your Ruby packages or libraries. This week I’m covering one of most popular RubyGems, Rails.

To setup Rails in your project, you need to have Ruby 1.9.3+ and RubyGems installed. Once you have met those requirements, you can run the following gem command in your terminal or console windows:

$ gem install rails

To verify that your installation was successful, type “$ rails –version”. It should display the word rails along with the version number. It is that simple.

What is Rails?

Rails is an open source web framework. It was originally created by David Heinemeier Hansson. It is a model view controller framework written in the Ruby language. Some of the companies using Ruby on Rails are: NASA, Amazon, YellowPages.com, Groupon, MadeWithEnvy, Heroku, Basecamp.

So what is different about Rails in comparison to other web frameworks? In my opinion, Rails allows you to build web applications faster because there is less configuration to perform. The frameworks also provides a default project structure. I don’t have to think where to place my models, views, controllers. By default, the framework handles those details for me.

To get started with Rails, I recommend the following resources:

1. The official Rails guide – http://guides.rubyonrails.org/getting_started.html

2. The Rails tutorial by Michael Hartl – https://www.railstutorial.org

3. Book – Agile Web Development with Rails 4

If you want to learn more about the latest developments with Rails, visit the github page at https://github.com/rails/rails. Rails is one of the most popular projects in github with more than 9441 forks, 24492 stars, and hundred of contributors.

I hope you find this brief introduction to Rails beneficial. See you soon.