Ruby Summary

  1. Avoid using Global scope and Global Variables. Global scope means scope that covers the entire program. Global variables are distinguished by starting with a dollar-sign ($) character. The Ruby interpreter starts up with a fairly large number of global variables already initialized. Global variables are distinguished by starting with a dollar-sign ($) character. The Ruby interpreter starts up with a fairly large number of global variables already initialized. Global variables don't mesh well with the ideals of object-oriented programming, as once you start using global variables across an application, your code is likely to become dependent on them. Because the ability to separate blocks of logic from one another is a useful aspect of object-oriented programming, global variables are not favored.
  2. gets (get a string) and chomp (a string method) are used to accept input from a user.
  3. gets returns a string and a '\n' character, while chomp removes this '\n'.
  4. STDOUT is a global constant which is the actual standard output stream for the program. flush flushes any buffered data within io to the underlying operating system (note that this is Ruby internal buffering only; the OS may buffer the data as well). The usage is not mandatory but recommended.
  5. To format the output to say 2 decimal places, we can use the Kernel's format method.
  6. Ruby Names are used to refer to constants, variables, methods, classes, and modules. The first character of a name helps Ruby to distinguish its intended use.
  7. Lowercase letter means the characters "a" though "z", as well as "_", the underscore. Uppercase letter means "A" though "Z", and digit means "0" through "9".
  8. A name is an uppercase letter, lowercase letter, or an underscore, followed by Name characters: This is any combination of upper- and lowercase letters, underscore and digits.
  9. You can use variables in your Ruby programs without any declarations. Variable name itself denotes its scope (local, global, instance, etc.).
  10. REMEMBER the way local, instance, class and global variables, constants and method names are declared.
  11. "?", "!" and "=" are the only weird characters allowed as method name suffixes.
  12. The Ruby convention is to use underscores to separate words in a multiword method or variable name. By convention, most constants are written in all uppercase with underscores to separate words, LIKE_THIS. Ruby class and module names are also constants, but they are conventionally written using initial capital letters and camel case, LikeThis. More examples: MyModule, MyClass, My_Constant, my_variable.
  13. Any given variable can at different times hold references to objects of many different types.
  14. Variables in Ruby act as "references" to objects, which undergo automatic garbage collection.
  15. For the time being, remember that Ruby is dynamically typed and that in Ruby, everything you manipulate is an object and the results of those manipulations are themselves objects.
  16. The basic types in Ruby are Numeric (subtypes include Fixnum, Integer, and Float), String, Array, Hash, Object, Symbol, Range, and RegExp.
  17. For the time being, remember that you can always see what object you are in (current object) by using the special variable self.
  18. We use def and end to declare a method. Parameters are simply a list of local variable names in parentheses.
  19. We do not declare the return type; a method returns the value of the last statement.
  20. It is recommended that you leave a single blank line between each method definition.
  21. Ruby allows parentheses to be omitted from most method declarations and / or invocations. In simple cases, this results in clean-looking code. In complex cases, however, it causes syntactic ambiguities and confusion.
  22. Methods that act as Boolean queries are often named with a trailing ?
  23. Methods that are "dangerous," or modify the receiver, might be named with a trailing ! (Bang methods)
  24. Ruby lets you specify default values for a method's arguments-values that will be used if the caller doesn't pass them explicitly. You do this using the assignment operator.
  25. For now remember that there is an interpolation operator #{...}
  26. alias creates a new name that refers to an existing method. When a method is aliased, the new name refers to a copy of the original method's body. If the method is subsequently redefined, the aliased name will still invoke the original implementation.
  27. In Ruby, we can write methods that can accept variable number of parameters.
  28. There's no limit to the number of parameters one can pass to a method.
  29. The sequence in which the parameters are put on to the stack are left to right.

Note: The Ruby Logo is Copyright (c) 2006, Yukihiro Matsumoto. I have made extensive references to information, related to Ruby, available in the public domain (wikis and the blogs, articles of various Ruby Gurus), my acknowledgment and thanks to all of them. Much of the material on rubylearning.github.io and in the course at rubylearning.org is drawn primarily from the Programming Ruby book, available from The Pragmatic Bookshelf.