CSC302 2011S, Class 02: Ruby (1) Overview: * Summary of Ruby concepts. * Typing. * Lab. Admin: * I'm still negotiating how to deal with the reading questions. Generally, I'll read them all to think about what to discuss, but won't cover everything. * Disclaimer: I am not an expert in any of the languages we are discussing. I certainly don't know about the underlying implementation details (although I can hypothesize a bit). * If you really care about how something is implemented, you could make it the topic for your presentation. * Reading questions due Thursday: Tate 2.3. * HW 1 is due Thursday! * EC for Microsoft Presentation, Thursday, 4:15 @ CDO. * EC for "Waiting for Superman", Thursday, 7:00 @ Harris Cinema. * No CS table this week Things you've learned how to do in Ruby * We can print strings with puts * Tests can be combined with and, or, &&, ||, &, | * The last two don't do short-circuit evaluation: They keep evaluating until they reach the end * Why short circuit? * Efficiency. Why do work that you don't need to do? * Safety: The second test might rely on the success of the first C programmers like to write things like if x && x->ok * Syntactic sugar: You could do without it, but it requires a lot more typing. * Why not short circuit? * So that people who don't understand short circuit evaluation can write code they can understand * If tests have side effects, you may want to ensure that all the side effects happen * Conditional statements * You can find out what methods are available for an object with object.methods * Two kinds of strings: "Foo" and 'Foo' Strings in double quotes get evaluated Random Questions * Is 3 + 4 syntactic sugar for 3.add(4)? * Yes Typing: * Static * Dynamic * Strong * Weak * Duck Debrief * How did you print your name ten times? 10.times {puts "Andrew"} * How did you print the ten sentences? x = 0 while x < 10 puts "This is sentence #{x}" x = x + 1 end (0..9).each {|x| puts "This is sentence #{x}"} * Why do languages commonly use zero-based indexing for arrays and strings? * It saves an operation in addressing * Are blocks first-class objects? How can you tell? If they are not, why not?