Ruby Strings

symbol what it does
+ concatenate
<< append
* repetition
[] indexing
<=> comparison, returns -1 for less, 0 for equal, 1 for greater
>> “a” <=> “b”
=> -1
>> “a” <=> “a”
=> 0
>> “b” <=> “a”
=> 1

Some more string methods, using examples:

>> s = “hello”
=> “hello”

>> s.concat(” world”)
=> “hello world”
# .concat is same as +

>> s.insert(5, ” there”)
=> “hello there world”
# inserts substring into part of the string

>> s.slice(0,5)
=> “hello”
# non-destructive slice, returns a substring

>> s = “hello there world”
=> “hello there world”
>> s.slice!(5,6)
=> ” there”
>> s
=> “hello world”

Note that the destructive slice! method deletes a section of the string itself (the actual object). same as s[5,6]=””. However, it returns the deleted substring, while altering the original string. (Notice that slice! in this case returned ” there” and s was changed to “hello world”)

>> s.eql?(“hello world”)
=> true
# same as ==
>> s.length
=> 11

In Ruby 1.8, counts the number of characters; in Ruby 1.9, counts the number of bytes.

>> s.size
=> 11

Size is same as length

>> s.bytesize
=> 11

Length in bytes; Ruby 1.9 ONLY

>> s.empty?
=> false
>> “”.empty?
=> true
>> s.index(‘w’)
=> 6
>> s.index(‘nothere’)
=> nil

Returns the position of a substring or pattern match; can also be used with regular expressions. Returns nil if no match is found.

Also try:

=> s.start_with?(“hell”)
>> true
=> s.end_with?(“world”)
>> true
=> s.include?(“ll”)
>> true

# test for presence of the substring within the string


Execute shell script from within ruby

1) Use the “system” method

system “ls”

2) Or use backticks. Note that backticks support you returning to the results and assigning them to a variable

res = `ls`

res now holds the directory listing returned by ls


Migration Generator Shortcuts

Some great shortcuts for script generation. I always try to start by building scaffold for any new entity, then remove the scaffold as needed, leaving only the pieces of the scaffold which I need.

to generate a new model:

script/generate scaffold Person age:integer first:string last:string some_list_id:integer

* This will generate models, controllers, migrations & tests. ALWAYS USE THE SINGULAR when using script/generate.

* Be sure to spell the complete word integer, if you mistakenly shorten it to abbreviated form “int” the generate command will look like it has worked, but migration won’t execute.

If you make a mistake try

script/destroy (model name)

to add a field to an existing model

script/generate migration add_address_to_person address:string

to add a foreign key to an existing model

script/generate migration add_state_id_to_person state_id:integer

Ruby function to check if something is a number

def is_a_number?(s)
s.to_s.match(/\A[+-]?\d+?(\.\d+)?\Z/) == nil? false : true

Rails Plugins with SVN

I’m working with a plugin that I wrote. I want to pull the plugin into an app I’m writing, but still be able to make changes to the plugin and commit those changes back to my repository.


Rake tasks

How can I search for available rake tasks?

rake -T | grep gem

(where “gem” is the thing you are searching for)

The above example will return all of the rake tasks relating to gems:

rake gems # List the gems that this rails application depends on
rake gems:build # Build any native extensions for unpacked gems
rake gems:install # Installs all required gems.
rake gems:refresh_specs # Regenerate gem specifications in correct format.
rake gems:unpack # Unpacks all required gems into vendor/gems.
rake gems:unpack:dependencies # Unpacks all required gems and their dependencies into vendor/gems.
rake rails:freeze:gems # Lock this application to the current gems (by unpacking them into vendor/rails)
rake rails:unfreeze # Unlock this application from freeze of gems or edge and return to a fluid use of system gems

Working with gems

Gems are installed by sources, or a repository which holds several gems. Generally all the gems you will need are either in or, so once you setup these two sources you don’t need to setup more sources until you need a gem from somewhere else.

How can I see the sources my gem is using?

gem sources

How can I add a source?

gem sources –add

How can I see the gems which are installed on my system?

gem list

What if I want to search for gems by name?

gem list -r | grep somegem

(where “somegem” is the name of the thing you’re looking for)

How can I see some information about my gem environment?

gem environment

How do I install all the gems needed for the app I’m working on?

rake gems:install