Skip to content

Latest commit

 

History

History
147 lines (117 loc) · 4.13 KB

21_ruby_101.md

File metadata and controls

147 lines (117 loc) · 4.13 KB

Ruby 101

  • Ruby — Programming language
  • Ruby on Rails — Web framework (think: Ruby's Django)

Syntax

  • 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}" and result = "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 }​

Names:

  • 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)

Hashes (something like dicts)

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

Control Structures

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

Manipulating strings

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 of Perl with Ruby.
  • newerline = newline.gsub(/Python/, ​'Ruby'​) will replace all occurrences of the word Python with Ruby.
  • newset = line.gsub(/Perl|Python/, ​'Ruby'​) will replace all occurences of the word Perl and the word Python with Ruby.