Learn Ruby 2011 - Session 2

744 views

Published on

In our second session we began the first half of our language crash course. This session covered Classes vs. Objects, Duck Typing, Variables, Strings and Methods.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
744
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
17
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Learn Ruby 2011 - Session 2

    1. 1. Learn Ruby 2011 Session 2
    2. 2. Our SponsorsFlatsourcing, iSeatz, Koda, and Launchpad
    3. 3. Welcome Back Ready to learn some Ruby?
    4. 4. Our Books
    5. 5. Learn to Program, 2ed• For total beginners• Systematic coverage of programming concepts• Uses Ruby to teach programming
    6. 6. 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
    7. 7. Language Crashing Learning the basics in pieces
    8. 8. 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
    9. 9. 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
    10. 10. Everything is an Object• In Ruby everything is an object• This means you can do things to everything you come across.
    11. 11. 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
    12. 12. 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
    13. 13. A Sample of Rubydef say_goodnight(name) result = “Good night, “ + name return resultendputs say_goodnight(“John-Boy”)puts say_goodnight(“Mary-Ellen”)
    14. 14. Variablesdef say_goodnight(name) result = “Good night, “ + name return resultendputs say_goodnight(“John-Boy”)puts say_goodnight(“Mary-Ellen”)
    15. 15. 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
    16. 16. Variables• There are four basic kinds of variables • local_variables • @instance_variables • @@class_variables • $global_variables
    17. 17. Variables• There are also Constants, variables that don’t change • ClassNames • CONSTANT_NAME
    18. 18. Variablesdef say_goodnight(name) result = “Good night, “ + name return resultendputs say_goodnight(“John-Boy”)puts say_goodnight(“Mary-Ellen”)
    19. 19. Stringsdef say_goodnight(name) result = “Good night, “ + name return resultendputs say_goodnight(“John-Boy”)puts say_goodnight(“Mary-Ellen”)
    20. 20. Strings• Strings in Ruby come in two basic kinds • “Double-Quoted” • ‘Single-Quoted’• They differ in how much processing Ruby does with their contents
    21. 21. ‘Single-Quoted’ Strings• Ruby does nothing with this kind of string, no interpolation, no escape processing, nothing
    22. 22. “Double-Quoted” Strings• Ruby performs additional processing of these strings • Ruby looks for escape sequences n t u2603 • Ruby also performs interpolation #{expression}
    23. 23. Stringsdef say_goodnight(name) result = “Good night, #{name}“ return resultendputs say_goodnight(“John-Boy”)puts say_goodnight(“Mary-Ellen”)
    24. 24. Methodsdef say_goodnight(name) result = “Good night, #{name}“ return resultendputs say_goodnight(“John-Boy”)puts say_goodnight(“Mary-Ellen”)
    25. 25. Methods• Methods are reusable bits of code• They also go by the name Functions.• They accept parameters, do something and return a value
    26. 26. Methods• Parameters are passed to methods and given convenient local variable names by the method definition
    27. 27. Methodsdef say_goodnight(name) result = “Good night, #{name}“ return resultendputs say_goodnight(“John-Boy”)puts say_goodnight(“Mary-Ellen”)
    28. 28. Methods• When calling methods the parentheses that surround parameters are optional.• Only leave them out when unambiguous
    29. 29. Methodsdef say_goodnight name result = “Good night, #{name}“ return resultendputs say_goodnight “John-Boy”puts(say_goodnight “Mary-Ellen”)
    30. 30. 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
    31. 31. Methodsdef say_goodnight name result = “Good night, #{name}“endputs say_goodnight “John-Boy”puts(say_goodnight “Mary-Ellen”)
    32. 32. Our Simplified Sampledef say_goodnight(name) “Good night, #{name}“endputs say_goodnight(“John-Boy”)puts say_goodnight(“Mary-Ellen”)
    33. 33. Reviewing• Objects vs. Classes• Duck Typing• Variables• Strings• Methods
    34. 34. For Next WeekFor the New to Programming• Read Chapters 2, 3 & 4 in LtP• Complete exercises for each chapterFor Everyone• Read Chapter 2 in PR1.9• Keep playing in IRB
    35. 35. Next Week• Arrays & Hashes• Symbols• Control Structures• Regular Expressions• Blocks & Iterators

    ×