T-14 to HW Due

Chapters 1-3 in Eloquent Ruby

Notes on Good practice

  • lowercase and underscores for all names, except Class Names, which should have uppercase and no underscores (8)
  • Constants can be either written like Classes, or all uppercase with underscores
  • A method with no arguments should have no parentheses (10)
  • semicolons can be used between statements to cram very simple methods and classes onto a single like (10-11)
  • A pair of braces can be used to delimit code blocks exactly like a do and an end, and is prefered when the code block has only a single statement
  • end method names with excalmation marks if the method does something unexpected (14)
  • if not can be replaced with the more idiomatic unless (18)
  • similarly, while not should be replaced with until (19)
  • don’t use a for loop, instead, use the each method (20)
  • the popular ternary operator ?: is used like so: file = all ? 'specs' : 'latest_specs' which means the same as: if file = all then 'specs' else 'latest_specs' (26)
  • Abreviate. @first = @first || '' (which means set the value of @first to @first unless it is equat to nil, in which case set it to an empty string) can be abreviated to @first ||= '' (but be careful: false will be treated as nil) (26)
  • This is a good one: instead of typing poem_words = [ 'twinkle', 'little', 'star', 'how', 'I', 'wonder' ] just do
    poem_words = %w{ twinkle little star how I wonder }
    , its the same thing (30)

Interesting things

  • a case statement is a giant value returning expression (22)
  • Virtually everything in Ruby returns a value (22), only false and nil are treated as false (23), everything else is true (including 0)
  • for this reason, avoid testing for truth, and test for specific values. There are several booleans in Ruby that don’t return true/false, like the defined? method (24)
  • if you use a hash in the same way you do in python, the keys will be symbols (30)
  • use *args (called a splat) to indicate that a method may take an arbitrary number of arguments (31-32)
  • map makes a new array, doesn’t modify original. works great for manipulating the elements of an array (35)
  • size and length seem interchangeable
  • the inject method is good for keeping track of some kind of overall value when running through a list, because it takes two arguments: each element of the list, and some “result” (36)
  • the reverse method creates a brand new array, but adding an exclamation point will let it write the reverse order to the array, like so: reverse!(37), same thing with sort and sort! (38)
  • hashes in Ruby 1.9 and up are ordered! (38)

Back to RubyMonks

I’m now getting the feeling that Ruby is a much more developed language than the Python 2.7 I was using; I tried using the each method to remove only the negative numbers from a string, got unexpected output (only deleted a few negative numbers each time it was run), looked for some online help, and could only find information about a tailor-made command for doing this (delete_if, and array.select { |each| each > 0} too) which worked perfectly. So, I’ll learn by searching and doing, really no point in reinventing the wheel, best to continue with RubyMonk.

  • the @ symbol in a class beginning a variable name signifies that it is an “instance variable of the class” (Monk)
  • yeild??? Here

Leave a Reply

Fill in your details below or click an icon to log in:

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