Ruby: Introduction (WIT-WSP)

part 1: basics

  • created by Yukihiro Matsumoto (Matz) in 1993.
  • popularised by Rails web framework (created by DHH) in 2005.
  • many inspirations: Smalltalk, Lisp, Python....
  • pure oo language (even the literals are objects).
    • 'hello'.contains('e'); 5.next();
  • used for: web apps, text processing, ai and math research.
  • open-source.

part 2: object-oriented

  • what is object? an instance of a class :P;.
    • object has a uniq identifier #object_id
    • object allocated with the .new method
    • object instantiated from the #class.
  • variables are objects? yes!
    • dynamic typing - variable's type is inferred.
    • instance variable holds object's state, store in object.
    • instance variable name start with @
    • class variable shared by all instances of a class
    • class variable name start with @@
  • methods are objects? maybe yes~
    • the #method can convert symbol to Method object.
    • methods define the object behaviour, store in class.
    • instance methods define within a class, can access obj's state directly.
    • class methods define with class's singleton class, invoked by class.
    • inheritance symbol is visualisable and funny < also means less than..
    • use comparison operators to check the inheritance: < <= == >= >.
    • module methods for sharing methods across multiple classes.
  • everything is object.muuu~
    • in java: Int num = Math.abs(-123)
    • in ruby: num = -123.abs

part 3: ruby scripts

  • do not require class definition.
  • no main() method needed.
  • execute with $ ruby test.rb

part 4: naming conventions

  • global variables are prefixed with a dollar sign ($)
  • instance variables are begin with (@)
  • class variables are begin with (@@)
  • local variables, method name should start with lowercase letter.
  • class names, module names, and constants must start with uppercase letter.
  • method name end with ? : method return a boolean.
  • method name end with ! : method do something danger.
  • method name end with = : related to an assignment statement.

part 5: strings

  • single quoted vs double quoted.
  • single quoted is like java string.
  • double quoted from supports expression interpolation.
  • expression inside #{...} is evaluated and the result inserted into string,.
  • arbitrarily complex expressions are allowed inside #{...}

part 6: collections

  • most common collections: array, hash
  • array and hashes are indexed collections.
  • both store collections of objects. accessible using a key/index.
  • array index(key) is an sequential integer start with 0.
  • hash index(key) can be any objects.
  • both array and hash grow as needed.
  • both array and hash can hold objects of diff types.

part 7: array

  • you can create and initialize a new array using []
  • you can also use Array.new method which may support more options.
  • lots and lots of built-in methods such as: reverse, reverse!...
  • special shortcut for creating an array of words: %w

part 8: hash

  • hashes are indexed using the square bracket notation.
  • hash return nil by default when indexed a key which doesn't exist.
  • you can set default value with: Hash.new(0) or hash.default = 0;

part 9: nil

  • nil is an object of NilClass
  • always means false when used in condition expressions.
  • all other ruby values are true (except the value false of course)

part 10: symbols

  • in programming you often need to create a name for something significant,
    • e.g. referring to compass points by name
      • NORTH=1; SOUTH=2; player.run(NORTH);
  • most of the time, the actual numeric values of these constants are irrelevant.
  • in java we use the global constant as the solution.
  • in ruby we use the new feature call symbol to solve it.
    • simply constant names; no declaration needed; uniqueness is guarantee.
    • a symbol literal starts with a colon, followed by some kind of name;
      • ....player.run(:east); car.turn(:south);......
    • no need to assign a value to a symbol/
    • guarantees the particular symbol has the same meaning,.
    • special syntax in hash: {a: 'a', b: 'b'} same as {:a => 'a', :b => 'b'}

part 11: control structure

  • usual control structures - if, while, until
  • keyword end is used instead of braces as the delimiter.
  • statement modifiers: single line control flow
    • puts 'danger!' if radiation > 3000

part 12: code blocks

  • in ruby, method's parameters can be data or block
  • code block behaviours as well as state can be passed around.
  • syntax: code blocks are enclosed in braces or do/end keywords.
  • convention: use braces for single-line block, otherwise use do/end style.
  • a block must be the last argument of the method.
  • a method invoke its associated block one or more times with 'yield'.
  • blocks are used throughout the ruby library to implement iterators.
    • pass each element to the block for processing.

part 13: dynamic typing

  • a variable's type does not need to be declared explicitly.
  • ruby is dynamically typed. its runtime determines a variable's type
    • based on its initialisation and how it is used(duck typing)
    • duck typing: if it walks like a duck, it is a duck!. # respond?
  • static versus dynamic typing
    • static: like java, better runtime optimisation, but slower development time.
    • dynamic: like ruby, greater developer productivity, but more testing required.

part 14: class

  • attr_reader(getters), attr_writer(setters)
  • attr_accessor(both getters and setters)
  • these are ruby library methods for create getters and setters.

你可能感兴趣的:(Ruby: Introduction (WIT-WSP))