learn ruby 2011 - session 2
TRANSCRIPT
![Page 1: Learn Ruby 2011 - Session 2](https://reader038.vdocuments.site/reader038/viewer/2022110111/55557802b4c9058a5a8b4e9f/html5/thumbnails/1.jpg)
Learn Ruby 2011Session 2
![Page 2: Learn Ruby 2011 - Session 2](https://reader038.vdocuments.site/reader038/viewer/2022110111/55557802b4c9058a5a8b4e9f/html5/thumbnails/2.jpg)
Our SponsorsFlatsourcing, iSeatz, Koda, and Launchpad
![Page 3: Learn Ruby 2011 - Session 2](https://reader038.vdocuments.site/reader038/viewer/2022110111/55557802b4c9058a5a8b4e9f/html5/thumbnails/3.jpg)
Welcome BackReady to learn some Ruby?
![Page 4: Learn Ruby 2011 - Session 2](https://reader038.vdocuments.site/reader038/viewer/2022110111/55557802b4c9058a5a8b4e9f/html5/thumbnails/4.jpg)
Our Books
![Page 5: Learn Ruby 2011 - Session 2](https://reader038.vdocuments.site/reader038/viewer/2022110111/55557802b4c9058a5a8b4e9f/html5/thumbnails/5.jpg)
Learn to Program, 2ed
• For total beginners
• Systematic coverage of programming concepts
• Uses Ruby to teach programming
![Page 6: Learn Ruby 2011 - Session 2](https://reader038.vdocuments.site/reader038/viewer/2022110111/55557802b4c9058a5a8b4e9f/html5/thumbnails/6.jpg)
Programming Ruby, 3ed
• Comprehensive langauge reference
• Covers Ruby 1.9.2, annotations for 1.9 and 1.9.2 specific features
• de facto standard reference
![Page 7: Learn Ruby 2011 - Session 2](https://reader038.vdocuments.site/reader038/viewer/2022110111/55557802b4c9058a5a8b4e9f/html5/thumbnails/7.jpg)
Language CrashingLearning the basics in pieces
![Page 8: Learn Ruby 2011 - Session 2](https://reader038.vdocuments.site/reader038/viewer/2022110111/55557802b4c9058a5a8b4e9f/html5/thumbnails/8.jpg)
Objects vs Classes
• An Object is a discreet thing
• You can do things to Objects
• Classes tell you how to build Objects
• Classes in Ruby are also Objects
![Page 9: Learn Ruby 2011 - Session 2](https://reader038.vdocuments.site/reader038/viewer/2022110111/55557802b4c9058a5a8b4e9f/html5/thumbnails/9.jpg)
Objects vs Classes
• A Car is a Class
• My Car is an Object
• My Car has lots of things in common with other cars
• So, My Car is a Car
• But, Not every Car is My Car
![Page 10: Learn Ruby 2011 - Session 2](https://reader038.vdocuments.site/reader038/viewer/2022110111/55557802b4c9058a5a8b4e9f/html5/thumbnails/10.jpg)
Everything is an Object
• In Ruby everything is an object
• This means you can do things to everything you come across.
![Page 11: Learn Ruby 2011 - Session 2](https://reader038.vdocuments.site/reader038/viewer/2022110111/55557802b4c9058a5a8b4e9f/html5/thumbnails/11.jpg)
Everything has a Class
• Because everything is an Object, everything also has a class
• An Object’s class is often referred to as it’s Type
![Page 12: Learn Ruby 2011 - Session 2](https://reader038.vdocuments.site/reader038/viewer/2022110111/55557802b4c9058a5a8b4e9f/html5/thumbnails/12.jpg)
Duck Typing
• Ruby is Duck Typed
• This means that when you encounter an Object in Ruby, what you can do to it determines it’s type
• Ruby’s “Type Model” is concerned with what you can do with Objects
![Page 13: Learn Ruby 2011 - Session 2](https://reader038.vdocuments.site/reader038/viewer/2022110111/55557802b4c9058a5a8b4e9f/html5/thumbnails/13.jpg)
A Sample of Ruby
def say_goodnight(name) result = “Good night, “ + name return resultend
puts say_goodnight(“John-Boy”)puts say_goodnight(“Mary-Ellen”)
![Page 14: Learn Ruby 2011 - Session 2](https://reader038.vdocuments.site/reader038/viewer/2022110111/55557802b4c9058a5a8b4e9f/html5/thumbnails/14.jpg)
Variables
def say_goodnight(name) result = “Good night, “ + name return resultend
puts say_goodnight(“John-Boy”)puts say_goodnight(“Mary-Ellen”)
![Page 15: Learn Ruby 2011 - Session 2](https://reader038.vdocuments.site/reader038/viewer/2022110111/55557802b4c9058a5a8b4e9f/html5/thumbnails/15.jpg)
Variables
• Variables hold values, in Ruby they hold Objects
• Variables are the handles we use to easily reference the data we want to work with
![Page 16: Learn Ruby 2011 - Session 2](https://reader038.vdocuments.site/reader038/viewer/2022110111/55557802b4c9058a5a8b4e9f/html5/thumbnails/16.jpg)
Variables
• There are four basic kinds of variables
• local_variables
• @instance_variables
• @@class_variables
• $global_variables
![Page 17: Learn Ruby 2011 - Session 2](https://reader038.vdocuments.site/reader038/viewer/2022110111/55557802b4c9058a5a8b4e9f/html5/thumbnails/17.jpg)
Variables
• There are also Constants, variables that don’t change
• ClassNames
• CONSTANT_NAME
![Page 18: Learn Ruby 2011 - Session 2](https://reader038.vdocuments.site/reader038/viewer/2022110111/55557802b4c9058a5a8b4e9f/html5/thumbnails/18.jpg)
Variables
def say_goodnight(name) result = “Good night, “ + name return resultend
puts say_goodnight(“John-Boy”)puts say_goodnight(“Mary-Ellen”)
![Page 19: Learn Ruby 2011 - Session 2](https://reader038.vdocuments.site/reader038/viewer/2022110111/55557802b4c9058a5a8b4e9f/html5/thumbnails/19.jpg)
Strings
def say_goodnight(name) result = “Good night, “ + name return resultend
puts say_goodnight(“John-Boy”)puts say_goodnight(“Mary-Ellen”)
![Page 20: Learn Ruby 2011 - Session 2](https://reader038.vdocuments.site/reader038/viewer/2022110111/55557802b4c9058a5a8b4e9f/html5/thumbnails/20.jpg)
Strings
• Strings in Ruby come in two basic kinds
• “Double-Quoted”
• ‘Single-Quoted’
• They differ in how much processing Ruby does with their contents
![Page 21: Learn Ruby 2011 - Session 2](https://reader038.vdocuments.site/reader038/viewer/2022110111/55557802b4c9058a5a8b4e9f/html5/thumbnails/21.jpg)
‘Single-Quoted’ Strings
• Ruby does nothing with this kind of string, no interpolation, no escape processing, nothing
![Page 22: Learn Ruby 2011 - Session 2](https://reader038.vdocuments.site/reader038/viewer/2022110111/55557802b4c9058a5a8b4e9f/html5/thumbnails/22.jpg)
“Double-Quoted” Strings
• Ruby performs additional processing of these strings
• Ruby looks for escape sequences\n \t \u2603
• Ruby also performs interpolation#{expression}
![Page 23: Learn Ruby 2011 - Session 2](https://reader038.vdocuments.site/reader038/viewer/2022110111/55557802b4c9058a5a8b4e9f/html5/thumbnails/23.jpg)
Strings
def say_goodnight(name) result = “Good night, #{name}“ return resultend
puts say_goodnight(“John-Boy”)puts say_goodnight(“Mary-Ellen”)
![Page 24: Learn Ruby 2011 - Session 2](https://reader038.vdocuments.site/reader038/viewer/2022110111/55557802b4c9058a5a8b4e9f/html5/thumbnails/24.jpg)
Methods
def say_goodnight(name) result = “Good night, #{name}“ return resultend
puts say_goodnight(“John-Boy”)puts say_goodnight(“Mary-Ellen”)
![Page 25: Learn Ruby 2011 - Session 2](https://reader038.vdocuments.site/reader038/viewer/2022110111/55557802b4c9058a5a8b4e9f/html5/thumbnails/25.jpg)
Methods
• Methods are reusable bits of code
• They also go by the name Functions.
• They accept parameters, do something and return a value
![Page 26: Learn Ruby 2011 - Session 2](https://reader038.vdocuments.site/reader038/viewer/2022110111/55557802b4c9058a5a8b4e9f/html5/thumbnails/26.jpg)
Methods
• Parameters are passed to methods and given convenient local variable names by the method definition
![Page 27: Learn Ruby 2011 - Session 2](https://reader038.vdocuments.site/reader038/viewer/2022110111/55557802b4c9058a5a8b4e9f/html5/thumbnails/27.jpg)
Methods
def say_goodnight(name) result = “Good night, #{name}“ return resultend
puts say_goodnight(“John-Boy”)puts say_goodnight(“Mary-Ellen”)
![Page 28: Learn Ruby 2011 - Session 2](https://reader038.vdocuments.site/reader038/viewer/2022110111/55557802b4c9058a5a8b4e9f/html5/thumbnails/28.jpg)
Methods
• When calling methods the parentheses that surround parameters are optional.
• Only leave them out when unambiguous
![Page 29: Learn Ruby 2011 - Session 2](https://reader038.vdocuments.site/reader038/viewer/2022110111/55557802b4c9058a5a8b4e9f/html5/thumbnails/29.jpg)
Methods
def say_goodnight name result = “Good night, #{name}“ return resultend
puts say_goodnight “John-Boy”puts(say_goodnight “Mary-Ellen”)
![Page 30: Learn Ruby 2011 - Session 2](https://reader038.vdocuments.site/reader038/viewer/2022110111/55557802b4c9058a5a8b4e9f/html5/thumbnails/30.jpg)
Methods
• Methods return values can be either explicit or implicit
• Use the return keyword to make explicit what your methods return
• Or, the last expression in the method will be the methods return value
![Page 31: Learn Ruby 2011 - Session 2](https://reader038.vdocuments.site/reader038/viewer/2022110111/55557802b4c9058a5a8b4e9f/html5/thumbnails/31.jpg)
Methods
def say_goodnight name result = “Good night, #{name}“end
puts say_goodnight “John-Boy”puts(say_goodnight “Mary-Ellen”)
![Page 32: Learn Ruby 2011 - Session 2](https://reader038.vdocuments.site/reader038/viewer/2022110111/55557802b4c9058a5a8b4e9f/html5/thumbnails/32.jpg)
Our Simplified Sample
def say_goodnight(name) “Good night, #{name}“end
puts say_goodnight(“John-Boy”)puts say_goodnight(“Mary-Ellen”)
![Page 33: Learn Ruby 2011 - Session 2](https://reader038.vdocuments.site/reader038/viewer/2022110111/55557802b4c9058a5a8b4e9f/html5/thumbnails/33.jpg)
Reviewing
• Objects vs. Classes
• Duck Typing
• Variables
• Strings
• Methods
![Page 34: Learn Ruby 2011 - Session 2](https://reader038.vdocuments.site/reader038/viewer/2022110111/55557802b4c9058a5a8b4e9f/html5/thumbnails/34.jpg)
For Next Week
For the New to Programming
• Read Chapters 2, 3 & 4 in LtP
• Complete exercises for each chapter
For Everyone
• Read Chapter 2 in PR1.9
• Keep playing in IRB
![Page 35: Learn Ruby 2011 - Session 2](https://reader038.vdocuments.site/reader038/viewer/2022110111/55557802b4c9058a5a8b4e9f/html5/thumbnails/35.jpg)
Next Week
• Arrays & Hashes
• Symbols
• Control Structures
• Regular Expressions
• Blocks & Iterators