Ruby Data Types

Numbers:

Integers:

An integer is a sequence of digits. In ruby that is represented as follows:

0
123
12345678901928373648273762527293002029828776236765238
1_000_000_000
0377          # Octal
0b1111_1111   # Binary
0xFF          # Hex
255

There are two varieties of integer in Ruby, Fixnum and Bignum. In most implementations of Ruby a Fixnum will utilize 0-31 bits and the Bignum will handle anything above that. Ruby will automatically translate integers from Fixnum and Bignum depending on the number of bits needed to make up the given number.

Float:

A floating point number is just like an integer with the exception that it contains a decimal point:

0.0
-123.45
1_000_000_000.01
60.02e33
255.00

You must provide numbers before and after a decimal. If the value is .001 it must be 0.001.

Your own git server

Why would you want to set up your own git server? I don’t know, maybe because all of your projects otherwise would have to be public (if you use the infamous github.com). This way you won’t have to keep paying more and more as the number of projects you have grow from 1-100. If you are interested in setting something up like this then this link is the only link you will need.

Posted in Git

Model Error

I had an interesting error today wile trying to use a model:

TypeError: wrong argument type ActiveRecord::Reflection::AssociationReflection (expected Class)

I did not know what the error was, looked over the model for any association errors and over the migration file. Nothing was wrong. Decided to try and run

rails destroy model :name
rails generate model :name

There obviously was an issue when the initial generate model command was ran as it now worked fine.. Just an FYI to anyone having the same issue.

Ruby Bundler

What is it?

Bundler is a Ruby gem that helps you make sure you have all of the gems your application needs, no matter what environment you are working in. In short, it tracks your application dependencies for you.

Why do I need it?

Bundler has made handling your gems easy. You use bundler to save you time when running your application on multiple servers (development, staging-testing, production).

How do I use it?

Bundler will use what is called a Gemfile that it stores in your document root. This file is where you list all of the gems your file will need. It must always start with a source location (of which you can declare more than one). Also notice that if your require statement is different than your gem name you need to include the :require declaration.

source "http://rubygems.org"

gem "rails", "~> 3.1.0"
gem "rack-cache", :require => "rack/cache"
gem 'authlogic'
gem 'ruby-debug19'
gem 'resque'
gem 'carrierwave'
gem 'rmagick'

You will then need to tell bundler to sync your gems.

bundle install

After you run ‘bundle install’ you will notice a new file called ‘Gemfile.lock’. This is a snapshot of all the gems your application is set to use. This file is used to rebuild your gem dependency tree.

Bundler with Sinatra:

Bundler works great with Rails, it also works with any other Ruby development tool. Sinatra is a good example of something that you will want to be able to use Bundler with. You have to actually tell the Sinatra application that you want Bundler to be involved.

# Running at the command line in the root of your application.
# Creates the Gemfile.

bundle init

# This is required at the top of your application.

require "rubygems"
require "bundler/setup"

# Require all of the gemsets.
Bundle.require(:default)

# Running at the command line in the root of your application.
# Brings in the gem files you need.

bundle install

Grouping:

Keep in mind that you can group together your gems in order to fit your environment. This will help you separate what each environment is meant to do. You do not always want to share the same gem packages with your production environment that is in your development environment.

group :development do
     gem "rails", "~> 3.1.0"
     gem "rack-cache", :require => "rack/cache"
     gem 'authlogic'
     gem 'ruby-debug19'
end

group :production do
     gem "rails", "~> 3.1.0"
     gem "rack-cache", :require => "rack/cache"
     gem 'authlogic'
     gem 'ruby-debug19'
     gem 'resque'
     gem 'carrierwave'
     gem 'rmagick'
end

That is what I have come to understand about Bundler. As with everything else you will want to familiarize yourself with the documentation on this gem. As always if you have something else to add or notice a problem with this post, feel free to make a comment. I don’t know everything…

Installing Ruby

Installing Ruby has apparently been a very hard thing to do in the past. I am new to Ruby at this point and to be honest I have only had one issue, of which a quick Google search resolved. I am using Ubuntu 11.04 as my development environment.

RVM:

RVM (Ruby Version Manager) is your best friend when it comes to Ruby installs. It will let you install multiple version of Ruby without any conflicts. This is good if you want to test out applications that are tied to a specific version that happens to be older than the currently released version. You will want to visit the website to read the documentation, but you can install it by entering the following command.

bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)

Once you have RVM installed it is a good idea to make sure you have all the requirements to install the version of Ruby you need. You can do that with the following command:

rvm requirements

You will want to copy the command under the version of Ruby you want to install and run it at a terminal. This will ensure you have all the necessary libraries installed. After you have done this you are ready to install Ruby. I am installing 1.9.2 as that is the current release at the time of writing.

rvm install 1.9.2

At this point you are good to go. This is a very high level overview of what you need to do. You will want to visit the developers website in order to get the full details. He has explained everything I have here in more depth. This post, much like all my posts; is just a snippet of something I will most likely forget.

Ruby – variables

Variables:

  • Are case sensitive.
  • Must start with a letter or an underscore.
  • Must contain only letters, underscores, and number.
  • May not contain any spaces.
  • Variables starting with a capital letter are CONSTANTS.
  • Must not use Ruby keywords.
Some examples:
# Valid:
x = 1
_x = 2
_X = 2
this_is_a_var = true
I_am_a_constant = true

# Invalid:
2x = false
this-is-a-var = false
my_!var@ = false
i am a variable = false

# Class variables:
x = 1      (local variable)
@x = 2     (object variable)
@@x = 3    (class variable)
$x = 4     (global variable)

One important thing to remember when working with Ruby is that everything is an object. it allows you to do things like this:

"I am a string".split(" ")

Issues with Ruby irb (interactive ruby).

I was wanting to play around with Ruby today so I figured I would go ahead and install it. Ruby seemed to install fine on Ubuntu 11.04(natty) through “rvm”. However, when I tried to bring up the irb console it gave me some errors. The helpful error message told me to check out https://rvm.beginrescueend.com/packages/readline/ to correct the issue.

I figured I would save (hopefully) someone some time as that did not work for me. If you are still having issues running irb after you follow the instructions on the helper site, take a minute to see if you have the proper libraries installed (it should give you a hint as to what you need in the error message). In my case I needed to install libreadline5-dev.

sudo apt-get install libreadline5-dev
rvm remove <version>
rvm install <version>

The above worked for me. If you have any more advice please feel free to comment. I don’t know everything (this is a learn as I go adventure…Ruby that is).

If it is complaining about zlib when trying to install a gem see this