Create
Learn
Share

Ruby

rename
Updated 2010-07-06 15:17

Summary

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

Basics

QuestionAnswer
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"
memorize

Functions and Structures

Question Answer
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()
memorize

Lists

QuestionAnswer
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
memorize

Classes and Objects

Question Answer
Create a class
class Hello
def salute
puts "Hello!"
end
end
memorize

Frameworks

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

Ruby on Rails

Rails is the most popular framework for creating webapps with ruby.
Question Answer
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)
memorize

Pages

See also

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

Recent badges