Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to Ruby

Introduction to Ruby

2011 CCSP

Andrew Liu

March 26, 2012
Tweet

More Decks by Andrew Liu

Other Decks in Programming

Transcript

  1. Introduction to Ruby
    Andrew Liu

    View Slide

  2. Ruby is…
    • Designed by Yukihiro Matsumoto (松本行弘)
    – "I wanted a scripting language that was more
    powerful than Perl, and more object-
    oriented than Python. That's why I decided to
    design my own language“
    • An object-oriented scripting language
    • Easy to learn!

    View Slide

  3. Philosophy
    • Often people, especially computer engineers, focus
    on the machines. They think, "By doing this, the
    machine will run faster. By doing this, the machine
    will run more effectively. By doing this, the machine
    will something something something."
    • They are focusing on machines. But in fact we need
    to focus on humans, on how humans care about
    doing programming or operating the application of
    the machines. We are the masters. They are the
    slaves.

    View Slide

  4. View Slide

  5. Naming Convention
    • CamelCase
    – Class names, module names
    • ALLCAPITAL
    – Constants
    • words_with_underscore
    – Method names, variable names
    • $ prefix
    – Global variables

    View Slide

  6. Everything is an object!

    View Slide

  7. Strings

    View Slide

  8. String Methods

    View Slide

  9. Array

    View Slide

  10. What if I want to reverse the array?
    http://ruby-doc.org/core/

    View Slide

  11. View Slide

  12. Range

    View Slide

  13. Hash

    View Slide

  14. Block

    View Slide

  15. How does it work?

    View Slide

  16. More About Class

    View Slide

  17. More About Class

    View Slide

  18. Class
    • Variable
    – @@ prefix: Shared by all class instances
    – @ prefix: Contains instances information
    – No prefix: normal variables
    • Methods
    – self. Prefix: Class methods, as known as static
    method
    – No prefix: Instance methods, must be called with
    a instance

    View Slide

  19. Creating Instance

    View Slide

  20. View Slide

  21. Flow Control

    View Slide

  22. Cases

    View Slide

  23. While

    View Slide

  24. For

    View Slide

  25. Resources
    • Programming Ruby (Bible)
    – http://www.ruby-
    doc.org/docs/ProgrammingRuby/
    • Ruby API Document
    – http://ruby-doc.org/core/
    • TryRuby
    – http://tryruby.org/
    • Ruby Warrior
    – https://github.com/ryanb/ruby-warrior

    View Slide