Tuesday, September 21, 2010

Ruby

http://railstutorial.org/chapters/rails-flavored-ruby#top

Ruby loop
$ rails console --sandbox
> a = %w[12@bc.com the_user@bcc.ca bla@blah.com]
> a.each do |a|
> puts a
> end
12@bc.com
the_user@bcc.ca
bla@blah.com
=> ["12@bc.com", "the_user@bcc.ca", "bla@blah.com"]

blocks
>> (1..5).each { |i| puts 2 * i }
2
4
6
8
10
=> 1..5

>> (1..5).each do |i|
?> puts 2 * i
>> end
2
4
6
8
10
=> 1..5

>> ret = (1..5).map { |i| i**2 } # The ** notation is for 'power'., stores value to 'ret'
=> [1, 4, 9, 16, 25]

so basically, symbols, eg. :name and instances @name does not throw exceptions

You can modify classes:
>> class String
>> # Return true if the string is its own reverse.
>> def palindrome?
>> self == self.reverse
>> end
>> end
=> nil
>> "deified".palindrome?http://railstutorial.org/chapters/filling-in-the-layout#top
=> true

Setting up rspec and sqlite3 can be a pain if you're using wrong versions while trying to follow tutorials ...

No comments: