Jun 092009
 
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

 Posted by at 10:35 am  Tagged with:
Jun 022009
 

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
 Posted by at 7:22 pm  Tagged with:
Jun 022009
 

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
 Posted by at 12:43 pm  Tagged with:
Jun 022009
 

Gems are installed by sources, or a repository which holds several gems. Generally all the gems you will need are either in gems.rubyforge.org or gems.github.com, 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 http://gems.github.com

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
 Posted by at 12:39 pm  Tagged with: