T-9 until assessment00 window closes

Ruby Bits 2

  • Level 1
    • Procs are for storing some kind of function, that will be called later with a yield in some other block
      • can be done by: name = Proc.new do ; # code here; end .And then called with name.call, and remember that a proc being called can take an argument that it would do something with: i.e. name.call(operand) is fine
      • if you make a lambda with name = lambda do ; # code here ; end , it can be called the same way, with name.call
        • shortcut for lambda is ->
      • procs make the invisible-like argument passing of a block to a method containing a yield, into a visible passing of a variable assigned to a block, which will then be called. (could thereby pass in multiple procs at once, though)
    • Some methods expect block objects (like each and map), if you’ve assigned some block to a proc my_proc, then pass that proc as an argument with an ampersand like: array_of_stuff.each(&my_proc)
      • you can also define a method to expect procs: def method(&var), (that is convert a block into a proc) and then call it within the method with another ampersand – this might be particular to the each method…not sure
      • The ampersand can come in handy if want to call a pre-existing method within an iterator
        • instead of doing list.map { |each| each.method1 } you can now do list.map(&:method1) where the method’s name as a symbol will be the name of the method searched for
          • the only disadvantage is that you cannot call methods on the symbol
    • in a constructor, by making an object yield itself in the initialize method, it makes itself available to being constructed in a block
      • you can do: Thing.new do |object| ; object.parameter = "high" ; end  as long as in the class’s def initialize you do something like yield self if block_given?
    • they call it Closure: cool thing you can do is first decorate a lambda with a method that takes a parameter and puts it inside a lambda. then, the method can be called with a parameter, making a version of a lambda. That particular version of the lambda can be called by giving some object to it, creating outputted possibilities with 2 degrees of difference
  • Level 2
    • Struct.new(:var1, :var2) gives the same functionality as making a class, using attr_accessor :var1, :var2 , and assigning local variables to the instance variables in the initialize method.
      • additional methods can be added via a block after the Struct constructor
      • good with data handling, rather than behavior
    • alias_method :new_method, :old_method makes a copy of a preexisting method. Great for “reopening”  methods, just make a copy of the old one, and restate it in the new one. But at the same time… you could just make a new class, inherit from the old, and call super from within the redefined and repeated method in the new class
    • define_method is the way that you would create a bunch of really similar methods
      • when using &block with it, things get confusing

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