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
Android Beginners

What is Android?

androidLogo

Android is a mobile operating system. It was originally created by a company called Android. In 2005, Google purchased Android for $50 million. It was a great move by Google. Right now Google is leading the mobile industry along with Apple.

The latest version of Android is called Lollipop and it was released on June 2014. To see the entire Android versions, go to the official Android page that contains more details about each version.

Since Android is being developed as an open source project, many companies are taking advantage of this and have developed their own version of Android. For example, Sony Ericsson and Motorola started developing their own mobile operating system but when Android started gaining momentum, they decided to use Android as their base for their new phones.

In later articles, I will cover in more detail the many features of Android 5 Lollipop.

Categories
Beginners ruby

Get familiar with the Ruby programming language

RubySyntax

Ruby is one of my favorite programming languages. Everything in Ruby is an object. In this article, I want to give a brief introduction to the Ruby syntax.

Strings

In Ruby, you can create string objects in the following ways:

first_name = "Mary"
last_name = String.new 

Now first_name can use the built-in methods available for the String class. For example, we can say first_name.length and we will get 4. Length returns the number of characters contained in that variable. In addition to the length method, the String class has other methods such as upcase, downcase, capitalize.

Numbers

In Ruby, you will encounter 2 types of classes that deal with numbers, floats and fixnums.

Float

Numbers that contain decimals are treated as floats in Ruby. To create a float, you can use:

tax_rate = 8.25
total_amount = 1234.67 

Fixnum

If you need to use integer numbers, you will use fixnum. The good thing about Ruby is that you don’t have to declare the class type. You can just assign any value to your variable and Ruby will handle it.

work_days = 5
work_days.class # Fixnum   

In the above example, typing work_days.class returned “Fixnum” which is the class use to hold integer values.

Array

The official Ruby documentation defines it as, “Arrays are ordered, integer-indexed collections of any object.” You can create Array variables with the following syntax:

a = Array.new
b = [1, 2.3, "test"]

In the above example, a is created by calling the new method on the Array class. b on the other hand is created by using the literal constructor. To get the first item in b, you can use b[0]. Since Array are zero index based, 0 represents the first item in the array.

Hash

A hash is a dictionary-like collection. It is made up of keys and values. The key must be unique. Take a look at the following examples:

h1 = Hash.new
h2 = { "Dallas" => 1, "Houston" => 2, "San Antonio" => 3 }

The h1 variable was created using the new method and h2 was created using the literal constructor. To get the value of the key “Dallas” in h2, you can type h2[“Dallas”]. To get the keys only, you can use h2.keys and this will return an array with only the keys. Similar to the keys method, you can get the values with h2.values. The hash class has a lot of methods that I will go into more detail in a future article.

I hope you find this quick introduction to the Ruby programming language helpful.

Categories
Beginners code ruby

Hour of code with Ruby

hour of code with ruby

Ruby is a “dynamic, open source programming language with a focus on simplicity and productivity”. That’s the definition taken from the official ruby site. Ruby is a fun and easy language to code in. Recently, I heard of hour of code, a movement to introduce computer science to million of students.

With that in mind, I want to share an excellent site to learn ruby. Go to tryruby.org and follow the instructions to start writing ruby code. There is nothing to install and you get clear instructions and instant feedback.

For example, you can type “2 + 2” and then press enter in the interactive window, you will see “4”.

Go ahead and have fun learning ruby.

Categories
.Net ASP.NET Beginners

How I learned .Net

I graduated from Southern Methodist University in 2001 with a bachelors degree in Management Information Systems. During my studies, I took classes in HTML, Javascript, Visual Basic 6 and Java. I interviewed with different companies but was unable to secure a full-time job in IT. So I decided to help my parents with their small furniture store in Dallas, TX.

While working at the furniture store, I learned so much about running a business. I learned about marketing, sales, accounting, managing people, and of course about IT. However, my desire was to work as a software developer. So I decided to learn .Net framework and specifically asp.net.

At the beginning I read many articles online about this new framework created by microsoft. .Net framework was the buzz word and I knew I had to learn it to secure a job as a software developer.

It was time to take action and start writing code. The online articles helped me a lot to understand the basics but I needed a mentor, a guide to write my first .net application. To accomplish that task, I bought this book:

Beginning ASP.NET 1.1 with Visual C# .NET 2003 by Wrox.

With the help of this book, I was able to setup my development environment and create my first asp.net site for the furniture store.

I highly recommend the books by Wrox specially the beginning series because they walk you step by step in learning a new programming language or framework.

I hope this article inspires other developers to start learning and creating projects.