Notes From Learning Ruby, Part 1 -Basics

spike_and_rarity__s_heart_shaped_fire_ruby_by_edwardten-1024x1000I’ve recently been learning Ruby, and I thought I’d share some of the notes I wrote. This will be in two parts, first the basics, then more advanced topics such as Blocks (when I get around to typing up those notes!).

The notes are sparse, and are coming from the perspective of someone who knows Java or some other language.

  • Ruby was developed in Japan, and took longer to be adopted compared to python
  • Before Javascript -JS took many ideas from Ruby
  • 2.even?
  • There is no need for return, the last expression is the return value
  • All methods return something, if just a nil object
  • Nil, never void
  • Classes start with upper case
  • Constants start with uppercase (this is how you denote a constant in the language), and by convention are all uppercase
  • Fixnum, String, Symbol, Object
  • Variables hold an object
  • No primitives
  • Nil is an Object
  • 2==3 is false
  • === is the containment equals, not the same thing as === in Javascript
  • == works the same as === in Javascript (eg it checks for type) ??????
  • == is an object in itself
  • kind_of/is_a are both the same and take into account object hierarchy
  • Ruby is not fast, and not for heavy duty, performance critical applications
  • Philosophy:
    • computer hardware is cheap, software is expensive to develop, therefore a non-performant language that is easy to develop in is optimal
    • Maximise developer happiness -that’s why there are so many ways of doing something (I could argue against this, but still…..)
  • Double quoted strings allow interpolation, you can add backslashes etc
  • Single enough strings are literal, and can’t be interpolated, nor can you add backslashes/quoted
  • %Q{ this is my text} translates to “this is my text”, %q to ‘this is my text’ (with single quotes).
  • % notation is useful for large amounts of inline text where you you’d otherwise need to do a lot of escaping, and there are many qualifiers (such as Q or q) you can use to do different things
  • All string are mutable in Ruby, there are no immutable strings
  • regex example “red” =~/red returns 0 (first index of match)
    • The 0 (and any non nil, non negative index) is considered ‘escapes’
    • enough means, while not ‘True’, can take the place of true in some contexts, such as if statements
    • so the above regex with “red” could be used as the evaluation for an if block
  • Symbols are labels/identifiers, and are as fast as numbers in how they are used
  • Think of symbols as user defined numbers, or rather as the numbers 1,2,3 etc as special language specific symbols
  • Symbols are in many ways similar to enums
  • Arrays are used for everything; there are no lists, sets etc
  • You can mix types in arrays, eg each element can be of a different type.
  • There are something like 200 methods on arrays, so you can do things to make them resemble, say, a set (by removing duplicates0
  • Can’t check for duplicates in array itself.
  • We also have Hash
  • Hashes are key value pairs
  • keys can be any type, but common to use symbols
  • normally something like {:apple => 05} but newer versions of Ruby support JSON style syntax \{apple: 0.5\}
  • Ranges in array \[-2..-5]
  • for i in 0….array.length
  • you can reverse the if:
    • do labels if X
  • Unless is a keyword too, good for guard statements
  • We also have case statements

That’s all for now folks! Let me know your thoughts, or any questions you may have.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s