CS312 - Spring 2012 - Class 1

  • administrative

  • ruby
       - Language itself has been around since the mid-90s
       - grown in popularity though since mid-2000s with the introduction of ruby on rails
       - designed to be programmer friendly
       - shares a lot of features with Python though has also borrowed some from Perl and other languages
       - ruby is object-oriented. almost everything is an object (and for practical purposes, you can generally just think that everything in ruby is an object)

  • irb
       - ruby is an interpreted language (vs. compiled)
       - irb (interactive ruby) is an easy way to interact with ruby if you're trying things out
       - when you first run it, it waits for commands:

          dkauchak-15819:working dkauchak$ irb
          >>

       - like most languages, ruby has numbers, strings and booleans built-in
          - numbers
             >> 5
              => 5
          
             - when you type a statement into the ruby shell and hit return the statement is executed and it's value is displayed after "=>"
             - every statement or expression in ruby evaluates to an object! We'll see more examples of this as we go.
             
             - Questions:
                - Does ruby differentiate between integers and floats?
                - If it does, what does it do when you divide two integers?
                - What does the "^" operator do?
                

             >> 5.0
              => 5.0
             >> 5+5
              => 10
             
             - ruby has all of the standard mathematical operations
             - ruby has both integers and floating point numbers         

             >> 5/10
              => 0
             >> 5.0/10
              => 0.5

             - like other languages, when you divide two integers, you get integer arithmetic

             >> -10/7
              => -2
             >> 10/7
              =>       
             1

             - ruby rounds towards negative infinity

             >> 5 % 10
              => 5
             >> 5^2
              => 7
             >> 5 ** 2
              => 25
             
             - most of the operators are intuitive, but be careful about some like '^', which is actually xor

             >> 10.class
              => Fixnum
             >> 124123513452345234523452345234523452345234523452345.class
              => Bignum
             >> 10.0.class
              => Float
             >> -10.abs
              => 10
             >> 10.odd?
              => false
             >> -10.abs()
              => 10
             >> 10.odd?()
              => false

             - numbers are objects, which mean they have methods
             - in ruby, the parentheses are optional. people generally leave them off for simple expressions and include them in more complicated expressions for clarity
             - '?' is a valid character in method names
             - ruby uses "true" and "false" for booleans

          - strings
             >> "this is string"
              => "this is string"
             >> 'this is a string'
              => "this is a string"
             >> "this 'is' a string"
              => "this 'is' a string"

             - strings are specified with either single or double quotes
             - the different options allow you to include single or double quotes inside strings without escaping them         


             >> "this is a string" + " another string"
              => "this is a string another string"

             - you can concatenate strings

             >> "this is a string " + 7
             TypeError: can't convert Fixnum into String
                from (irb):73:in `+'
                from (irb):73
                from /Users/dkauchak/.rvm/rubies/ruby-1.9.3-p0/bin/irb:16:in `<main>'

             - but not strings and integers

             >> "this is a string " + 7.to_s
              => "this is a string 7"

             - you can either convert the number to a string using to_s which all classes support (kind of like toString in java except it isn't automatically called when a number is used in a String context)

             >> "this is a string %d" % 7
              => "this is a string 7"

             - or you can also use printf-type commands for doing formatted text
          
             >> "this is a string"[5]
              => "i"
             >> "this is a string"[-1]
              => "g"
             >> "this is a string"[1..5]
              => "his i"

             - you can index into strings
                - they're indexed starting at 0
                - if the index is negative, it counts from the back
                - you can "slice" the string using a sequence (e.g. "1..5"), which grabs the strings starting at the first number and up to and including the last number


             >> "this is string".class
              => String

             - strings are objects have LOTS of methods
                - see the documentation http://ruby-doc.org/core-1.9.3/String.html

             >> "this is a string".split
              => ["this", "is", "a", "string"]
             >> "this is a string".length
              => 16
             >> "this is a string".upcase
              => "THIS IS A STRING"
             >> "this is a string".squeeze
              => "this is a string"
             >> "this is a string".center
             >> "this is a string".end_with?("string")
              => true

  • writing ruby programs
       - we can also write ruby programs inside a text file and run them
       - run my_first_script.rb .rb
          - to run a program from the command-line we just type ruby and then the name of the file
             dkauchak-15819:examples dkauchak$ ruby my_first_script.rb

       - look at my_first_script.rb .rb
          - what are some of the things that you notice that are interesting/unusual about the code?
          - ruby programs start at the top of the file and work their way down executing each statement one at a time
          - # indicates a comment
             - we will put blocks of comments at the top of any file, at the top of any class definition and at the top of any functions. These conventions will allows us to automatically generate documentation down the road
             - like any other programming language, you should also comment confusing parts in your code
          - puts and print
             - print out to the console
             - puts includes a newline (i.e. carriage return) while print does not
          - you can nest any ruby statement/expression inside a string by escaping it with "#{...}"
             - #{name} just inserts the value of the name variable
             - #{name.length} calls the length method on the name variable and will insert this value
          - parentheses are not required
          - blocks of code are started with keywords (e.g. while, if, ...) and then ended with "end"
             - we will indent code in the same block
             - however, it is not required (unlike in Python)
          - gets waits for the user to enter some text and then hit return
             - Be careful! it includes the carriage return
          - if statement works as you would expect

  • look at name_analysis.rb .rb
       - what does this program do?
          - checks to see if the correct number of command-line arguments were supplied
          - ARGV is an array with the command line arguments
       - we've defined two functions
          - to define a function, you use "def", then the name of the function and then the arguments, separated by a comma
          - you can either include parenthesis are not around the arguments
          - like any other block, the code is terminated with and end
          - since everything in ruby represents a value, even if you don't include a "return" statement your function will still return a value
             - it returns the value of the last statement in the function
       - =~ is regular expression search
          string =~ /regular_expression/
             - will return the starting index of where the pattern first matches, if it matches
             - returns nil if it doesn't match