Posted over 3 years ago. Visible to the public.

Ruby basics

Ruby is the programming language we use almost exclusively.

Goals

  • Be able to read and write simple ruby programs.
  • Gain an understanding of the following concepts:
    • Basic datatypes: Strings, Numbers, Booleans, Arrays, Hashes
    • Basic language constructs: if, each, case, break
    • Regular expressions
    • Functions: def, return
    • Object orientation
      • Classes
      • Class methods vs instance methods (def self.method vs. def method)
      • Inheritance
      • Modules and include
    • Code blocks
    • Input and Output
  • Know where to find documentation for ruby's standard library.

Resources

Look at these

References

  • apidock.com: Reference for Ruby standard libraries
  • devdocs.io: DevDocs combines multiple API documentations in a fast, organized, and searchable interface

Exercises

Word count

Write a small ruby programm cw that accepts a filename, counts the number of
words, lines and paragraphs, and outputs the result.

Hints:

  • Find out about ARGV.
  • Look up File on apidock.
  • Learn about regular expressions.

random_each

Give Array a new method .random_each, that loops randomly through the entries.
I want to be able to say

Copy
[1, 2, 3, 4, 5].random_each do |value| puts value end

And get

Copy
4 2 1 5 3

Hints:

  • Find out about "class reopening" aka. "monkey patching" (e.g. http://www.runtime-era.com/2012/12/reopen-and-modify-ruby-classes-monkey.html <- expired)
  • Find out about yield

Owner of this card:

Avatar
Henning Koch
Last edit:
5 months ago
by Thomas Tasler
Posted by Henning Koch to makandra Curriculum
This website uses cookies to improve usability and analyze traffic.
Accept or learn more