CSC302 2006S, Class 37: Presentations (1) Ruby Admin: * Sorry for not giving proper support to today's walkout. * Student presentation today (Brown, Nettling, Tasev). * I'll take notes (and insert comments) on the eboard. * Late: Tasev, Ventresca Overview: * An interesting program * Ruby design goals * Strings in Ruby * Numbers in Ruby /An Interesting program/ while gets if /Ruby/ print end if /break/ break end end Purpose: Reads lines from input. Prints lines that contain the word Ruby. Quits when user enters "break". Interesting aspect: Includes a default variable (used in the pattern match, the print, and elsewhere). [Seems like Perl] [Did not demonstrate program in action.] /Goals of Ruby/ * Tries to be an object-oriented language in every way * Iterators, objects, inheritance, etc. * Everything in Ruby is an Object * Also tries to act like a scripting language * "System" calls * Full support for pattern matching * Immediate feedback * Easy and natural to use * Natural syntax * Garbage collection * Dynamic typing /Strings/ * Strings are a basic type in Ruby * Many built-in functions * Concatenation ruby> name = "sam" "sam" ruby> name += " rebelsky" "sam rebelsky" ruby> name << ", professor" "sam rebelsky, professor" * Read line from keyboard: gets * Includes the carriage return (terminating character) * Remove it with chomp * Comparison with <=> * 1 is greater than * -1 is less than * 0 is equal [run with "ruby /home/nettling/CS302/presentation/ruby.rb"] * Capitalization operations * capitalize (capitalizes the first word) * swapcase (switches case) * downcase * upcase * Length with length * Substring with include name.include? "sam" * Silly things * reverse * next (treats like counter) * Can affect substrings, indicated by brackets [Dimitar seemed somewhat unsure of what he wanted to say next. More rehearsal would be nice.] * To learn more, inspect the class, call its "methods" method [Question: How do you declare methods with a special syntax, such as the brackets?] /Numbers/ * Pretty cool * Lots of built-in methods * They are objects * Float and integer (Fixnum) [Would have liked to have seen a comment that Fixnum is used instead of Integer] * Factorial program def fact(n) if n == 0 1 else n * fact(n-1) end end * fact 1189 creates a stack overflow [Did not think about whether the overflow as "too large number" or "too many recursive calls". I think the latter.] * Integers seem to be arbitrarily large; real numbers are approximated * Some interesting methods: * floor * ceil * round * Alternate notations * Scientific: 1.23e10 * Hex (begins with 0x) * Binary (begins with 0b) * Octal (begins with 0) [Side note: Ruby returns the last thing evaluated.] [A little more on the evaluation model and things like introspection would have been useful] * Control * times 7.times {|i| print i} * downto (counts from the number down) 10.downto(0) {|i| print i, " "} [{|i| print i } is like (lambda (i) (print i)] * upto (similar) * step(val,inc) * counts using a step /Syntax issues/ * Identation and whitespace are irrelevant * In the interpreter, newline is "execute the line" * Comments begin with a pound * Rarely need to parenthesize parameters * Special characters and typing [Peter recited, but without text, it's hard to remember.] /Typing Issues/ * You rarely need to explicitly type a variable * You can determine the type of something [How? Using the .class method] * You can change types dynamically [How?] * Constants begin with capital letters, but they're not really constants (you just get a warning that you're changing them) ruby> Capital = "washington" "washington" ruby> Capital = "Ottawa" (eavl):1: warning: already initialized constant Capital * Cast with to integer "to_i" method or Integer(...) /Arrays/ * Are objects, just like everything esle in Ruby * Are heterogeneous * The + operation is concatenation ruby> ary = [1,2] + 3 [1,2,3] * Matrices and other multidimensional arrays by array of array of * Arrays are dynamic ruby> ary[4] nil ruby> ary[4] = 3 3 ruby> ary[4] 3 * Supports ranges ary[1..3] [Hmmm ... wouldn't it have been better to introduce the array notation before discussing strings] [Not sure how it's implemented] /Hash Tables/ ruby> h={"dog"=>"canine", "cat"=>"feline"} {"dog"=>"canine", "cat"=>"feline"} ruby> h["dog"] "canine" [Didn't leave a lot of time for questions]