DRY Ruby Initialization with Hash Argument

You don’t need the constant, but I don’t think you can eliminate symbol-to-string: class Example attr_reader :name, :age def initialize args args.each do |k,v| instance_variable_set(“@#{k}”, v) unless v.nil? end end end #=> nil e1 = Example.new :name => ‘foo’, :age => 33 #=> #<Example:0x3f9a1c @name=”foo”, @age=33> e2 = Example.new :name => ‘bar’ #=> #<Example:0x3eb15c @name=”bar”> … Read more

How to use mixins properly in Javascript

A mixin is just a different conceptual idea, of how to organize code and inheritance. You can of course combine it with using classical or prototypal inheritance, but it also works stand-alone, so to speak. For instance, instead of creating “delegated” object properties/lookups (like prototypal inheritance), we would truly “form” new stand-alone objects, from multiple … Read more

What’s the recommended way to extend AngularJS controllers?

Perhaps you don’t extend a controller but it is possible to extend a controller or make a single controller a mixin of multiple controllers. module.controller(‘CtrlImplAdvanced’, [‘$scope’, ‘$controller’, function ($scope, $controller) { // Initialize the super class and extend it. angular.extend(this, $controller(‘CtrlImpl’, {$scope: $scope})); … Additional extensions to create a mixin. }]); When the parent controller … Read more