Starting Ruby On Rails - Basics

From Logic Wiki
Jump to: navigation, search


To learn the methods of an object put .methods at the end of it

1.methods
1.methods.sort

Methods can take arguments

2.between?(1,3)

Array

words = ["foo", "bar", "baz"]
words[1]

Building strings

a = 1
b = 4
puts "The number #{a} is less than #{b}"

It is conventional in Ruby to have '?' at the end of the method if that method returns only boolean values.

"I can't work with any other language but Ruby".end_with?("Ruby")

To replace first instance :

"I should look into your problem when I get time".sub('I','We')

To Replace all :

"I should look into your problem when I get time".gsub('I','We')

Regular Expressions or RegExs are a concise and flexible means for "matching" particular characters, words, or patterns of characters. In Ruby you specify a RegEx by putting it between a pair of forward slashes (/). Now let's look at an example that replaces all the vowels with the number 1:

'RubyMonk'.gsub(/[aeiou]/,'1')