T-6 to HW due

Ruby Bits 1

  • Level 4 – Activesupport (installed activesupport and i18n gems)
    • helper methods for arrays
      • from(array_element) inclusively splices a section of an array from an element to the end – to(array_element) does the reverse, also inclusively
      • in_groups_of(number) breaks an array into groups of some number
      • split(array_element) non inclusively splits an array at the given element
    • for DateTime
      • manipulate a DateTime object with at_beginning_of_day or at_beginning_of_month, and advance(args) where you can pass years: 4, months: 2, and tomorrow and yesterday.
    • for hashes
      • hash1.diff(hash2) gives the difference between two hashes
      •  can also stringify_keys
      • hash1.reverse_merge(hash2) will merge two hashes, prioritizing hash1 if there are key/value pairs that exist in both
      • except removes a key
      • assert_valid_keys will throw an exception if there are keys in a hash that aren’t explicitly allowed
    • Other
      • each_with_index allows iteration with both each element and its index being available
      • the ordinalize method is called on an integer in a string to add an ordinal suffix (1st, 5th)
        • similarly, pluralize and singularize methods are available for strings
        • and the titleize string helper (humanize adds spaces and capitalizes)
    • Finding more methods and browsing
      • after installing the gem-open gem, at the terminal, type gem open activesupport
      • navigate to the directory $GEM_HOME/gems and choose a gem, and then search through its directory for a keyword using finder
      • or read the railsguide
  • Level 5 – Modules
    • good way to have methods be required by a separate ruby file, without them just loading into the global namespace
    • Mixin intro – its common to have a Module with methods in one file, require it in another, and then include Module inside the class block, or extend Module
      • extend exposes module methods as class methods (just as if they were typed in the class) while include exposes module methods as instance methods
        • but if you use extend with an object via object.extend(Module), it will add Module‘s methods as instance methods on object, so if an object2 is created later from the same class as object was, will not have whatever instance methods were added into object
      • include leads to the module being included in the ancestor chain of the class (the ancestors method will list what classes a class is descended from – you can also call included_modules to see modules only)
      • In Ruby, a class can have only one superclass, so class inheritance cannot be used more than once, since it suggests specialization that you might not be interested in, so Mixin is useful for this reason
      • remember that instance methods (include) must be used on instances, while class methods (extend) must be used on the class itself.
    • So… so far its been an either/or situation for including instance or class methods.. but what about including both simultaneously?
      • you might nest a module NestedClassMethods inside of a module InstanceMethods and then do include InstanceMethods and then extend InstanceMethods::ClassMethods inside some other class
      • But, alternatively, since self.included(ModuleToInclude) is a method automatically called by Ruby when you say include ModuleToInclude in a class:
        • if you place a method like: def self.included(arbitrary_arg) ; arbitraty_arg.extend(NestedClassMethods) ; end within the InstanceMethods module, when you include InstanceMethods within some class, the NestedClassMethods module inside the InstanceMethods module will automatically be extended into that same class. This is a hook. And by the way, note that: arbitrary_arg is representing the module it is inside of. Because include will be called on the InstanceMethods module, the self.included will also be called on the InstanceMethods module, but the arg’s name doesn’t matter
          • you can also put additional calls while defining self.included(arbitrary_arg) that will run methods in the NestedClassMethods module
          • if you have the activesupport gem installed, you can require 'activesupport/concern', then, inside the InstanceMethods module, type extend ActiveSupport::Concern which will specifically look for the module named ClassMethods inside the InstanceMethods module, and allows for an included do; # put methods to run in ClassMethods here ; end block; which is executed when you run include InstanceMethods in some other class.
            • Example: def self.included(arbitrary_arg) ; arbitraty_arg.extend(NestedClassMethods) ; arbitrary_arg.method1 ; end
          • problems can arise when you have an include Module1 nested inside a Module2 (which contains methods dependent on Module1) and then you try to include Module2 in some class, because, the include Module2 argument will be passed along inside Module2 to where it tries to include Module1 (and self.included(arbitrary_arg) in Module1 will now represent Module2 instead of Module1, and get confused. By using extend ActiveSupport::Concern in Module2, you are ensuring that the right representative arguments will be passed to the self.included(arbitrary_arg) within the outermost Module1 (which will automatically be done and be invisible, if you are using ActiveSupport in both Module1 and Module2)
  • Level 6 – Blocks
    • when something is yielded within a block, it is as if that yield sends out its result to the meta-block calling the block, whereby the meta-block does some processing and sends its result back to the block right after the yield. By setting a variable equal to the thing being yielded, the result of the second method is brought back into the first
    • yield by itself, is sort of preparing a spot for some function/block: def func ; yield ; end and then a func do puts "ya" is sort of showing do puts "ya" as an argument to the func function.
    • yield with some object is very similar, the yield statement is preparing a spot for the final return of some other block, but its also sending that object, which can be picked up with the pipe bars | | and manipulated in some way

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