- Ruby — Programming language
- Ruby on Rails — Web framework (think: Ruby's Django)
- Everything's an object.
puts "something"
prints something.puts("something")
is the equivalent (think: Python 2.x vs. Python 3.x)- Classes start with
def <NAME>(<INPUT_PARAMS>)
and end with... well,end
. - Common practice in the Ruby community is to use two space indentation.
- When needing to print a value of a variable, both
result = "Good night, #{name}"
andresult = "Good night, " + name
should do the trick. - There's a difference between using single quotes and double quotes. Single quotes have no processing, double quotes do (you can't use something like
#{name}
or\n
with single quotes). a = [ 'ant', 'bee', 'cat', 'dog', 'elk' ]
==a = %w{ ant bee cat dog elk }
- Local variables:
something
kthxbye
,_x
,_26
- Instance variables:
@something
@kthxbye
,@x
,@_
- Class variable:
@@something
,@@x_pos
,@@sum
- Global variable:
$debug
,$plan9
,$CUSTOMER
,$_
- Class name:
Something
,Kthxbye
,String
,ActiveRecord
,MyClass
- Constant name:
FEET_PER_MILE
,DEBUG
Null
->nil
(object that represents nothing)
inst_section = {
'cello' => 'string',
'clarinet' => 'woodwind',
'drum' => 'percussion',
'oboe' => 'woodwind',
'trumpet' => 'brass',
'violin' => 'string'
}
p inst_section['oboe'] # -> Returns "woodwind"
p inst_section['cello'] # -> Returns "string"
p inst_section['bassoon'] # -> Returns nil
:name
symbol is used commonly to replace the usage of single quotes. So, the above could also be used as such:
inst_section = {
:cello => 'string',
:clarinet => 'woodwind',
:drum => 'percussion',
:oboe => 'woodwind',
:trumpet => 'brass',
:violin => 'string'
}
inst_section[:oboe] # -> Returns "woodwind"
inst_section[:cello] # => Returns "string"
inst_section['cello'] # => Returns nil, because `:cello` is not equal to `'cello'`
This is also a valid syntax:
inst_section = {
cello: 'string',
clarinet: 'woodwind',
drum: 'percussion',
oboe: 'woodwind',
trumpet: 'brass',
violin: 'string'
}
puts "An oboe is a #{inst_section[:oboe]} instrument"
# Outputs: An oboe is a woodwind instrument
Instead of the braces for the if
and while
loops, Ruby uses the keyword end
to mark the end of the loop.
if
example:
today = Time.now
if today.saturday?
puts "Do chores around the house"
elsif today.sunday?
puts "Relax"
else
puts "Go to work"
end
while
example:
num_pallets = 0
weight = 0
while weight < 100 and num_pallets <= 5
pallet = next_pallet()
weight += pallet.weight
num_pallets += 1
end
Ruby supports something called statement modifiers, so:
if radiation > 3000
puts "Danger, Will Robinson"
end
...is equal to:
puts "Danger, Will Robinson" if radiation > 3000
...and:
square = 4
while square < 1000
square = square*square
end
...is equal to:
square = 4
square = square*square while square < 1000
Ruby supports regex out of the box with the "match" parameter, so this is legit:
line = gets
if line =~ /Perl|Python/
puts "Scripting language mentioned: #{line}"
end
...and basically searches for Perl
or Python
to satisfy the criteria.
newline = line.sub(/Perl/, 'Ruby')
will replace the first occurrence ofPerl
withRuby
.newerline = newline.gsub(/Python/, 'Ruby')
will replace all occurrences of the wordPython
withRuby
.newset = line.gsub(/Perl|Python/, 'Ruby')
will replace all occurences of the wordPerl
and the wordPython
withRuby
.