CSE341 Section #9 Problems Summary of Ruby features: Types: numbers: Fixnum (3, 802), Float (3.4) booleans: true, false strings: "hello", "how are you?" arrays: [3, 15, "hello", [7, "howdy"], 308.4, true] ranges: 1..10, 'a'..'z' Constructs: class Foo [< ] end def f(p1, p2, ..., pn) end def f(p1 = v1, p2 = v2, ..., pn = vn) end Syntactic sugar for defining getters and setters: attr_reader symbol1, symbol2, ..., symbol-n attr_writer symbol1, symbol2, ..., symbol-n The constructor for a class always calls a method called initialize public/protected/private keywords used inside a class to mean "starting here, constructs have this kind of access" instance-variables start with @ class-variables (static variables) start with @@ symbols start with : if [then] elsif [then] else end while [do] end for in [do] end loop break if end .() { } .() do end can begin with |params| and has a sequence of statements embed an expression to be evaluated in a string: "text before #{expression to evaluate} text after" a method takes a block if it includes calls on yield nil is a special value that represents "no object" Useful methods arithmetic: +, -, *, /, %, ** (exponentiation), +=, -=, *= mathematical: Math.sqrt, Math.sin, Math.cos relational: <, >, <=, >=, !=, == logical: &&, ||, ! conversion: .to_f, .to_i, .round, .to_s random: rand, rand() Array/String: x.length # of elements x[i] element i (0-based indexing) x[i, n] slice with n elements starting at i x[i..j] slice with values at index i through j x << v append v to x x.sort sort values x.member? v does it contain v? x.index v index of v in x Arrays only push v appends v to end of array pop remove and return top value Strings only chomp used to eliminate trailing newline characters from string upcase uppercase version of string Input/Output puts x like Java println print x like Java print (stay on current output line) print x, y print multiple values gets reads a line of input File.open(s) returns a file object for given file .gets read a line from file object 1. Define a Ruby class called Point that can be used to store a pair of (x, y) coordinates. It should have the following public methods: Point.new(x, y) Constructs a new point with given x, y coordinates Point.new() Constructs a point with coordinates (0, 0) x, x= getter/setter for x y, y= getter/setter for y to_s string equivalent in the form: (x, y) 2. Define a Ruby class called Tree that stores a binary search tree of values. It should have the following public methods: Tree.new() Constructs an empty tree insert v adds v to the search tree print prints values, one per line, in sorted order In writing your class, you should define a private inner node class. 3. Using your code from 2, define a variable t that refers to an empty binary search tree. Then add 10 random values in the range of 0 to 99, echoing the values one per line with a message like "inserting 18". Then print the result. 4. How would you find out the class of 3.8? The class of the class of 3.8? The methods available for the class of the class of 3.8? The superclass of the class of 3.8?