T-12 Until HW Due

  • Olsen’s Chapter 11: Equality in Ruby
    • eql? is never called directly, but used in the hash method to decide if two keys in a hash table are equal (key that you are searching with is equal to key that you are looking for, using object_id), based on object identity (150)
    • equal? references identity: x.equal?(y) is only true if they reference the same exact object (143)
    • == is the same thing as equal? (143)
      • Note: floats and Fixnums (integers) are == to each other since a fixnum is converted to a float before it is compared
      • the <=> operator is the same, but 0 when both are equal, and 1 if the first is greater, and -1 if the first is smaller
    • === confusingly, same thing as == but is used in case statements (149)
  • Ruby Bits 1:
    • Level 1
      • stuff like this: if password.length < 8 ; print "crap" ; end is the same thing as this: print "crap" if password.length < 8
      • an if blocks within another if block can be replaced with an && between the two conditionals
      • thing ||= 1 means that thing will be set to 1 if it is not set right now
      • Assign less variables by using a conditional return value
      • the count method is an alternative to the length method for an array
      • case statements just seem like alternatives to if/then that minimize use of variables
    • Level 2
      • attr_accessor can be used to create class objects that can be referenced and changed later, use attr_reader if you want to only be set once and then uneditable
      • Seems as if after using attr_accessor :variable in a class, an instance variable can be declared with either @variable = variable or self.variable = variable, but the are different somehow
      • to build in a custom exception, import: class My_Error < StandardError, then put in a raise My_Error.new in a conditional within the method that you want to raise the exception. Then later in the code, when you want to access this error, write begin and in its block write the command you want to call, then write rescue => what_we_built_in and write a block with the string you want to print including the name of the error via the what_we_built_in or just write in some alternate code to execute
  • Handling Errors – Cooper’s Chapter 8
    • To make your own type of exception, just import the RuntimeError class into one that you make: class MyBadDataException < RuntimeError
  • Finishing RubyMonk Lessons
    • modules are like classes, but don’t contain states (although they can contain classes which will contain states), they can have methods, and can be imported into classes with the include Module_Name method
    • instead of opening a global class that doesn’t belong to you, or if multiple people work on identically named classes and methods individually (libraries), if you really want a class of the same name, make your own module with that class in it, and reference is with “namespacing” object = Module_Name::Class_Name.new
      • each time you use the :: you are indicating everything with the level that it follows (info)
    • IO is a class containing methods for all ins/outs into ruby like printing text onto the screen or recieving keyboard input or reading/writing to disk
    • Ways to open a file are indicated similarly to Python: infile = File.open("file_on_compy.txt", r+) (read/write). Calling different modes when opening the file will make different methods available
    • Ruby reads files like you play tapes, remembering where you stopped reading them, taking the in file.rewind method to go back to the beginning of the file before moving onto the next puts method
Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s