create
learn
share

Ruby

edit

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"
start learning
start learning

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()
start learning
start learning

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
start learning
start learning

Classes and Objects

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

Frameworks

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

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)
start learning
start learning

Pages

See also

http://ruby.codehaus.org/Quick+Start"
blog comments powered by Disqus