Categories
ASP.NET MVC C# ruby ruby on rails

How Ruby on Rails Taught me ASP.NET MVC

Before learning ASP.NET MVC, I had the opportunity to learn Ruby and Ruby on Rails. In this post I want to share how Ruby on Rails taught me ASP.NET MVC.

Managing Dependencies

To create a ruby on rails app, let’s type “rails new blog” in a terminal or command prompt. Now that we have a new app, we can see how rails manages dependencies. By running command “bundle install” inside the blog directory, Bundler will install all dependencies inside the Gemfile. It is that simple.

ASP.NET MVC has a similar process. We use nuget to install and manage dependencies. And our Gemfile is named packages.config. We also have a command line to install libraries (Install-Package jQuery).

App Structure

By convention, rails app creates the following directories inside the root app directory:  models, views, and controllers. ASP.NET also creates models, views, and controller but they are created at the root level. Rails gave me a solid understanding on the MVC architectural pattern. Before learning ASP.NET MVC, I knew that models were responsible for the domain’s entities. Views are only concerned with the presentation layer (UI). And controllers manage incoming requests and provide a link between models and views.

Routing

Before learning rails, I was using ASP.NET Forms. With this framework, we were not concern with routing because aspx files were served directly from the file system. There was no configuration done by us. The system just handle it. In the rails world, the route config is located in config/routes.rb. A typical route file might look like:

get "/posts" => "posts#index"
get "/posts/:id" => "posts#show"
get "/posts/new" => "posts#new"
post "/posts" => "posts#create"  # usually a submitted form
get "/posts/:id/edit" => "posts#edit"
put "/posts/:id" => "posts#update" # usually a submitted form
delete "/posts/:id" => "posts#destroy"

In ASP.NET MVC, you will see:


public class MvcApplication : System.Web.HttpApplication
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Default",                                              // Route name 
            "{controller}/{action}/{id}",                           // URL with parameters 
            new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
        );

    }

    protected void Application_Start()
    {
        RegisterRoutes(RouteTable.Routes);
    }
}

They are basically mapping a request to a controller action. After learning rails, it was very easy to me to pickup this new routing thing.

Summary
I’m always recommending that you learn a new programming language, framework, or tools. In my case, I decided to learn a new web framework called Ruby on Rails. Not only I learned a new programming language but also a new web framework. I never anticipated that by learning rails I was also gaining a solid foundation to learn and use ASP.NET MVC.

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.