Memorize.com

Ruby

edit

Summary

Memorize keywords, tips and code snippets for http://www.ruby-lang.org/en/, the scripting language.

Basics

Question Answer (memorize)
print a stringputs "hi"
run a ruby script (from the command-line)ruby hi.ruby
string with variable"hi #{s}"
string with expression"hi #{1 + 2}"
multi-line string"foo
bar"

Functions and Structures

Question (memorize) Answer (memorize)
Ruby term for "Map" (or Associative Array)Hash
if block
if(i == 5)
puts "five"
end
define a function
def hi(a)
puts a
end
invoke a function
hi("you")
define a class
class Hi
# Functions
end
instantiate a class
h = Hi.new()

Lists

Question Answer (memorize)
create a listl = ["foo", "bar"]
append to listl << "third"
iterate through listl.each{ |i| puts i }
join a list (into one string)l.join( "," )
access 2nd element (in a list)l[1]
shorthand for [3, 4, 5]3..5
double listl * 2

Classes and Objects

Question (memorize) Answer (memorize)
Create a class<pre>class Hello
def salute
puts "Hello!"
end
end

Frameworks

Framework (memorize) Description (memorize)
gemsruby packages
build toolRake
web app frameworkRuby on Rails
lightweight webapp frameworkCamping
RMagickmanipulate images (wraps ImageMagick)

Ruby on Rails

Rails is the most popular framework for creating webapps with ruby.
Question (memorize) Answer (memorize)
principles- don't repeat yourself - convention over configuration
scaffoldingskeleton code frameworks
database frameworkActiveRecord
LocomotiveRails installer (for OS X)
templates.rhtml files (via ERb - Embedded Ruby)

Pages

See also

http://ruby.codehaus.org/Quick+Start"