T-8 until assessment00 window closes

Ruby Bits 2

  • Level 3
    • Ruby uses self to know where to call methods – if you call a method without an object. preceding it, Ruby will try to apply the method to self
    • class_eval sets the identity of self to a given class and executes a block
      • could be useful if you want to add a class method to a class without reopening the class, set the identity via ClassName.class_eval and provide a block of code
    • when using define_method , we can pipe both arguments and blocks to a method that we may be executing within the block of the method that we are defining. This is done by: define_method method_name do |*args, &block| ; send("other_method", *args, &block) ; end
    • There’s also an instance_eval, call it on an object, and give it a block, and self will be set equal to an instance of that object
      • calling instance_eval will set self automatically in the block that you type, but it can also pipe self to the block, so you can either use self outright, or you can give it a variable name. Both are identical:
        • contra_game.instance_eval { self.owner = "Alice" }
        • contra_game.instance_eval { |itself| itself.owner = "Alice" }
      • if you wanted to yield self to a block in the initialize method of a class (so, that means you yield the instance object of the class), say to pipe the object into some custom methods during the creation of a class (obj = Tweet.new do |instance| ; # call methods on instance ; end) …
        • instead of using yield in the body of the initialize method, pass a block into the argument of the initialize method i.e. initialize(&block), and then, use instance_eval(&block) in its body to pass self as the instance of the class, upon which the code may then be executed upon… so the same custom methods stated later will work: (obj = Tweet.new do |instance| ; # call methods on instance ; end)
          • this is a more explicit way for a method to capture a block
  • Level 4
    • hooks = methods that Ruby calls when certain events happen
    • you can redefine method_missing to add some functionality, like printing the error into some log file, or we would make it send whatever method is missing to a particular variable which might have that method
      • there is also a SimpleDelegator class in Ruby for this purpose. Have your delegating class inherit from SimpleDelegator (after require 'delegate').
        • Example: If class2 inherits from SimpleDelegator and then a new instance of it is created with a class1 object as an argument, then it will be as if class2 < class1 and class2 will also have the same values for its instance variables as the class1 object had (helpful comment).
      • if you delegate a method and arguments to an instance variable, use the send method as usual, but include the splat in from of the arguments: @instance_var.send(method_name, *args)
    • we can ask an object: object.respond_to?(:method_name) to see if it’ll work with a particular method
    • with dynamic methods, where you defined a method if one (that did not yet exist) was called, with the define_method method. You will probably need to create a custom implementation of respond_to? if you want it to work in this case, by defining it to mirror the selection rules for the dynamically created methods (names from a constants list, or including a “hash_” prefix, or whatever…)
  • Level 5 – Domain Specific Language
    • Example: ActiveRecord (for database storage) – language with terminology for a specific domain
      • External – standalone – lots of work  – cucumber
      • Internal – implemented within another programming language
    • We may construct a class Klass and then define a method that will use that class and many methods in it, if we wanted to do a series of tasks with this class, we could define a big_method that creates a class object via obj = Klass.new, and then yields, so that we may then give a block with obj.method1 and obj.method2 and so on…alternatively it is possible to establish a &block argument in that big_method, and pass it into obj.instance_eval(&block) within big_method so that when we call big_method later and provide it a block (since we are executing it within the context of the obj), we don’t have to write obj.method1 but just method1
    • remember that if you want to implement chaining, a method that is followed up on, must start with the altered thing, so a chainable method should return self
  • After Ruby Bits
    •  interactive RubyKoans.com
    • practicingruby.com , $8 month newsletter
    • Books: practical OO design in ruby, DSLs by Martin Fowler, Confident Ruby
    • Scotland on Rails
    • Ruby5 podcast, Ruby Rogues podcast


  • Cooper
    • MVC
      • Models – Plato’s ideals – classes
      • Views – mix of HTML and minimal Ruby rendered for web Browsers
      • Controllers – provide logic, call methods in models, contain methods called actions
  • Next up: Michael Hartl‘s Rails Tutorial

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