Stephen Lloyd

Software Engineer

2/20/11Ruby, Chapter 4 - Arrays, Ranges and Hashes

A Hash is an Associative Array

 

2/18/11Ruby, Chapter 4 - Arrays, Ranges and Hashes

Puts vs Print

  • n=["a","b","c","d","e"]
    puts n.last(2)
    d
    e

    print n.last(2)
    ["d","e"]

Shuffle

  • n.shuffle creates a random order of the array

Unique

  • Use the uniq method to return only the non-repeated values
  • How many unique values? n.uniq.length

Reverse

  • puts ('a'..'z').to_a.reverse.join
  • the range is converted to an array, reversed, and converted to a string.
  • it outputs zyxwvutsrqponmlkjihfedcba
  • to_s converts an array to a string (in list form, comma delimited)
1/28/11Ruby, Chapter 4 - Arrays, Ranges and Hashes

Arrays

  • Array syntax: groceries = ['kiwis', 'cereal', 'milk'] also can be done this way: groceries = %w{hot mild cold}
  • 0-based. groceries[1] is cereal
  • A negative number counts back from the end, starting with -1. groceries[-3] is kiwis
  • to create a blank array: groceries = Array.new

Multi-dimentional Arrays

  • colors = [ ['Red', 'Green', 'Blue'] , ['Cyan', 'Magenta', 'Yellow'] ]
  • colors[1][2] is Blue, colors[2][0] is Cyan
1/23/11Ruby, Chapter 3 - Simple Types
  • Everything in Ruby is an object
  • Variable names are case sensitive
  • Methods that end with a ? return true or false. Methods that end with ! alter the value of the object.
  • integer type is Fixnum. floating point is Float. numbers are Numeric
  • parallel assignment: x,y = 1,2
  • multiple assignment: x=y=z=2
  • 20 / 7 = 2
  • 20.0 / 7 = 2.8571...
  • 20 % 7 = 6 ( the remainder of a division )
  • 3 **3 = 9 (3 to the power of 3)
  • rand 100 or rand(100) will generate 0-99, never 100.
    • use rand(100) + 1 to generate 1-100
    • use rand(101) to generate 0-100
  • 101.even? will return false
  • + concatenates Strings. << updates the variable with new content at the end.
    msg = 'world'
    msg << '!'
    puts msg
    world!
  • msg.object_id - every object has an id
  • qty = 5
    puts "the number you chose was #{qty}" (must be in double quotes to work)
  • to concatenate numbers in strings, they must be converted to strings. "The value of x: " + x.to_s
  • use %q{thius is "my" string} to create a multi-line string where you don't have to worry about escaping quotes.
  • A Symbol is assigned with a colon. car = :Toyota, priority = :high
  • Constants begin with a Capital letter, but by convention, they are written in all caps. DOZEN = 12
  • Dates and Times. Time.now, Time.now.year, Time.now + 60 adds 60 seconds
1/9/11Ruby, Chapter 2 - Simple Scripts
  • Ruby IDE's: SciTE, Netbeans
  • ruby 2getland.rb runs the program. ruby -w 2getland.rb runs it and shows warnings
  • puts is the most common output method. It automatically adds a new line at the end.
    print does not add a new line at the end.
  • \n in the output string within quotes adds a new line. \a adds a beep \t tab, \" quotes, \\ backslash, \r return
    \' only escapes ' when used inside single quotes
  • gets() asks for input and always returnsa string type
    • if you want to use the results of the gets as numeric, it must be converted.
      age = gets()
      ageNum = age.chomp.to_i
  • # is a comment
1/9/11Ruby, Chapter 1 - Getting Started
  • The Interactive Ruby window accepts simple commands like 2+2
  • To use the command prompt, use the launcher in the start menu: Start Command Prompt with Ruby.
    Then, Ruby --version will work. Oddly, 2+2 does not.
  • in the Start Command Prompt with Ruby shell: Ruby 1helloworld.rb
  • Use puts with double quotes to evaluate ruby commands within curly brackets. puts "Hello #{name}"
  • \a in the puts or print string adds a beep(alert) in the progam

puts / print

  • puts adds a new line at the end.
  • print does not. example: print "This land is " print "#{pnoun}" print " land"

 


Stephen Lloyd 11/23/2015