Posted in Ruby

Ruby pointers and passing references

Abe’s answer on this StackOverflow question about whether Ruby is pass-by-reference or pass-by-value is good.

Chuck in this other one clarifies further with another example.

Posted in Ruby

Copying Ruby objects: to dup or clone or “=”?

…That is the question!

The Stackoverflow discussion linked to here contains a number of useful bits and bobs with regard to elucidating the difference between the dup and clone methods, and also using “=” to copy a value.

Ruby – DUP vs CLONE

Both DUP & CLONE can be used to create shallow copy of an object. Both copies the instance variables of obj. But we need to be selective in their usage.Few difference between these are1) CLONE copies both FROZEN and TAINTED state of an object, where as DUP only copies TAINTED state of an object.2) With CLONE you can copy any singleton methods of an object but DUP does not support this.

CLONE is used to duplicate an object, including its internal state, DUP typically uses the class of the descendent object to create the new instance.

Open source is wide open

Operator “=” does not make a copy, shallow or tainted: it will merely copy the object reference.

A good article on Object states can be found here.

Posted in Ruby

Why use a Ruby symbol?

The Ruby Newbie Guide to Symbols is a decent introduction to symbols in Ruby, though it doesn’t get into how symbols actually are implemented within the system. In short, though:

  • The only reason that symbols could be found preferable over strings is because they save on data, this because every use of the same symbol refers to the same object. Even though the programmer refers to it with its name the system can find it by referring to its number, which results in a faster lookup.
    • Note however that Symbols of course will not have String methods, so are relatively deficient if what you really want is a string with all its inherent string functionality.
  • The things a programmer might find valuable about a using symbol is that it’s immutable (it can’t be changed on runtime) – which sometimes is exactly what you want. In this respect, they can be used as constants – perhaps if you really, really want a constant whose identifier doesn’t begin with a capital letter.
    • Arguments for taking such an approach? Because you benefit from abiding by variable-naming conventions (for example, Ruby doesn’t enforce the constancy of constants, but variables that start with a capital letter in Ruby are automatically constants, and Ruby does automatically and helpfully warn you if you try to change these), and a constant with a non-uppercase first letter breaks conventions and thus could cause confusion to people reading your code. As such, a symbol – which is by its nature immutable – is a good choice both in terms of being functionally what you’re after (ie you can’t change its value) and for communicating to readers of your code that the variable in question, even with its non-uppercase first letter, really is a constant.

The Difference Between Ruby Symbols and Strings also emphasises the way that symbols are immutable, and the more predictable results + performance you therefore get when using them.

Posted in Ruby

Setting up Ruby on Rails on OS X

First thing you’re going to need: Rob Conery’s quite excellent post here. It steps the new-to-OSX developer easily through the process of getting Ruby and Rails set up on your Mac using RVM – and without the gigantically cumbersome download of Apple’s XCode you’ll find in so many other instructions on doing this.

Initialising your database

Don’t forget to initialise your database as per the instructions given to you at the end of installation: if you skip this, your database won’t work. (If you’ve closed the terminal and need to see these instructions again, you can type “brew info mysql” to have them displayed.)

Setting up your root password

Start your server by typing mysql.server start to be able to log into it, then set the password up as per the Homebrew instructions.

Once this is all working, it’s a nice idea to get the thing starting up automatically whenever you boot up. The Homebrew instructions state: “To start mysqld at boot time you have to copy support-files/mysql.server to the right place for your system”. Details on where this is/how to do it are here.

Install Autotest and integrate with Rspec2

Michael Hartl’s tutorial helps with replicating his development setup, with Autotest continuously running your tests in the background based on your file changes and, in combination with Growl, providing instant feedback as you work. Install it as follows:

$ gem install autotest -v 4.4.6
$ gem install autotest-rails-pure -v 4.1.2

Install Growl if you don’t already have it installed, then install the autotest-fsevent and autotest-growl gems.

$ gem install autotest-fsevent -v 0.2.4
$ gem install autotest-growl -v 0.2.16

To use the Growl and FSEvent gems, make an Autotest configuration file in your application root directory, (or in your home directory if you want to run autotest across all your projects):

$ mate .autotest

and fill it with the below (ta flyerhzm, here) :

# Include plugins 
require 'autotest/fsevent' 
require 'autotest/growl'   

# Skip some paths 
  Autotest.add_hook :initialize do |autotest|   
  %w{.git .DS_Store ._* vendor}.each { |exception| autotest.add_exception(exception) }     

To get autotest picking up your rspec tests, create a directory “autotest” in your application root, add a directory called ‘autotest’ to the root of your application, and in it a file called “discover.rb” containing the following text:

Autotest.add_discovery { "rspec2" }

After that you should be able to run “autotest” from the root directory of your application and see your tests run.