Vinyl Siding Undone

Posted: January 7, 2012 by Steve in Uncategorized

20120106-191557.jpg

A very penguin Christmas

Posted: December 20, 2011 by Steve in Uncategorized

20111219-205724.jpg

Have you ever wondered how those DSL’s in ruby managed to get rid of the requirement to pass a variable to a block to indicate the context of the enclosed block? An example of this in Rails route definitions where the blocks are simply just nested. My question was how does each block know what it is nested inside of? After looking at actionpack I found the solution. It’s a little known (to me) function to execute an anonymous block in the context of an object: instance_exec. I have inserted it below with an example of its use. It is different than the one in activesupport, because instead of InstanceExecMethods.module_eval, I did self.class.instance_eval. The method below I got from apidock.com on the definition of instance_exec and exposing the source. The latest version of activesupport doesn’t seem to have it, so I don’t know where it resides now.

def instance_exec(*args, &block)
  begin
    old_critical, Thread.critical = Thread.critical, true
    n = 0
    n += 1 while respond_to?(method_name = "__instance_exec#{n}")
    InstanceExecMethods.module_eval { define_method(method_name, &block) }
  ensure
    Thread.critical = old_critical
  end
 
  begin
    send(method_name, *args)
  ensure
    InstanceExecMethods.module_eval { remove_method(method_name) } rescue nil
  end
end

My example just defines a tree of the Simpson’s. When a node is defined, its block is evaluated in the context of that new node object.

class Node
  def initialize(name=nil)
    @name = name
    @children = []
  end
 
  def node(name, &block)
    child = Node.new(name)
    @children.push(child)
    child.instance_exec(&block) if block
  end
end
 
def tree(name, &block)
  @tree = Node.new(name)
  @tree.instance_exec(&block)
  @tree
end
 
tree("Simpsons family tree") do
  node("gramps") do
    node("homer+marge") do
      node("bart")
      node("lisa")
      node("maggie")
    end
  end
end
 
puts "tree = #{tree.inspect}"

And the result is

tree = #<Node:0x1ee2433b @name="Simpsons family tree", @children=[#<Node:0x5c29ea31 @name="gramps", @children=[#<Node:0x578b1f8f @name="homer+marge", @children=[#<Node:0x408fbecf @name="bart", @children=[]>, #<Node:0x4cd4544 @name="lisa", @children=[]>, #<Node:0x153b2cb @name="maggie", @children=[]>]>]>]>

Funny thing that hubris

Posted: December 5, 2011 by Steve in Uncategorized
Tags:

About 5 weeks ago I mentioned to a friend of mine that my fantasy football team was a team without a weakness. I was undefeated, leading the league in points, and firing on all cylinders. He said to be careful, as he had just won his fantasy baseball league over a team like that. That team had lead the whole season until the final day, when they lost to my friend. Now my team has no cylinders firing at all, and I just dropped out of first place and may miss the playoffs. Funny thing that hubris!

programming and driving

Posted: December 2, 2011 by Steve in Uncategorized

Today I did something that sounds illegal, but actually wasn’t. You see I had a programming issue that was perplexing me and didn’t require a lot of typing. I had an opportunity to drive the slow way from downtown St. Paul to uptown Minneapolis and think about my problem. With some black rebel motorcycle cycle club blasting through the air and the nice sites of Selby ave wafting by, my mind could comfortably ponder the issue at hand. Everytime that I thought of something to try, I would just pull over type something in and try it. When it didn’t work, I would just go back to driving and pondering. By the third time I pulled over, I had it solved.

I think I have to do his more often!

Help me!

Posted: November 24, 2011 by Steve in Uncategorized

20111124-131218.jpg

Notice the drowning man in the orange destined for to drown in the Thanksgiving wassail

<3 me

Posted: September 25, 2011 by Steve in Uncategorized

20110925-111650.jpg

The main ingredient missing in our current news system is the systematic pursuit of truth. It is riddled with attempts to distract, obscure, mislead and confuse the public. The reasons are many and varied, but my thesis is centered on the idea that the powers that own or control the media have no interest in the pursuit of truth.

My proposal is to create a software eco-system to allow the formation of social eco-systems that are centered on the continuous pursuit of truth as events happen. A social eco-system that is focused on the continuous pursuit of truth relies on three principles: a desire to pursue the truth, a systematic method to do so, and a way to ensure that as much of the whole truth is exposed as is possible.

The desire to pursue the truth is the mission statement for these new ecosystems. Every decision in how they should evolve into the future can be made in relation to whether it will enhance the pursuit of truth or not.

The method best suited to systematically pursue the truth is the scientific method. The gathering of observations and the testing of hypothesis against those observations has served civilization for 400 years in the greatest expansion of knowledge the world has ever seen.  This is particularly important when assessing claims and counter claims about a particular issue (eg. how to address the deficit or if it needs to be a number one priority at all).

Finally, on issues of truth in the world, it matters who is doing the research and how their needs are being met. The scientific method may be our best tools to get to the truth, but it can be corrupted through the omission, distortion, and incorrect weighting of observations. This can be seen in actual scientific work where industry sponsored studies consistently result in different conclusions than non-industry sponsored studies. Who is in control of the needs (read: pay) of the people doing the researching, assessing, selecting of stories to pursue matters in what is produced as a final product. It can also be corrupted through the omission of entire areas of inquiry. This has resulted in promising solutions to current problems being ignored, and silence to the concerns of large swaths of people in need.

Both of these issues can be addressed through two characteristics: diversity and transparency. Diversity of funding is important to make sure that one or a small number of funding sources don’t effect how news is gathered. Diversity of writers/editors/contributors ensures that the stories that are covered are relevant to all parts of society. Transparency of the entire organization, including all accounting, pay, and agreements with other organizations will allow anyone to audit the pressures that influence this organization’s output.

Journalism is a process that happens everyday. It is only when we have a continuous focus on the process that we can be sure that the output of it approximates the truth.

Go here for an expanded version of this.

Towards a better news system

Posted: June 5, 2011 by Steve in Uncategorized

The following post was going to be my submission to the Mozilla-Knight Foundation challenge for People Powered News, until I realized that the limit was 500 words. My next posting was my actual submission.

Background

I follow Jay Rosen and Dave Winer and love their take on a new news system and how it might work. They have great ideas and great ideas can matter and change things. But often great ideas fail not for lack of their greatness but because of how they fit into the landscape of power in society. If the “powers that be” don’t see the advantage for them in accepting these ideas, and the “powers that aren’t” don’t champion them as a way to upset the current power structure, then they don’t go anywhere. In short, the best ideas don’t always or even often win.

When it comes to journalism (defined as a broadly as possible, involving not just journalistic professionals but anyone who wants to journal about life’s ongoing events), there are considerable impediments to improvement in terms of who is delivering news now and why they are delivering it in the way that they do. The process of improvement must start by illustrating what is broken with the current system of news. To see what’s broken, we need to see how it deviates from an ideal for a news system in society.

What’s Broken

The ideal system of news should supply the most timely, and accurate information to the people that need it to make decisions on how best to live their lives individually and collectively into the future.

The current system of news fails us in numerous ways.

  • it focuses on trivial matters that have little impact on the real lives of people (Charlie Sheen, etc)
  • on substantive issues, it doesn’t seek to clarify, but just report what both “sides” say (he said/she said journalism)
  • news is reported without context so that it the understanding of important events is relayed without any historical reference
  • what counts as news is often determined by frames set up by official sources
  • more and more news coverage sourcing comes from PR firms that either generate stories or provide slanted content (for the benefit of their clients) to provide framing for the story.

The Truth Matters

What underlies much of this is a lack of focus on getting to the “truth”. Much of the way news is covered is really about avoiding and obscuring unpleasant truths that the powers that be would rather that the public didn’t know or understand.  It is not clear whether the type of news produced is the result of demand or supply. That is, do we get the news system we demand or the news system that is provided to us? Since, there is competition between news outlets, someone should have stepped up and provided us better more accurate news that I am looking for. On the other hand, there are overarching powers in society such as government and business that constrain unpleasant truths to the margins of news coverage. Part of what brought down the regimes in Tunisia and Egypt were releases of truthful information so long hidden by their governments and and by the US government. Since that release of truth, there has been a large backlash by the US government for further truth telling by the severe prosecution of whistle blowers (read: Bradley Manning). The powers that be tolerate the current news system because it gives the appearance of a news system that works when it actually doesn’t.

My Principles

My emphasis on power structures leads me to my suggestion for a component of the rebooted news system (to use Jay Rosen & Dave Winer’s phrase): a citizen owned, power balanced, model of news collection with a focus on using power balance to get at the truth. That is a mouthful, but in essence the pursuit of truth is a three legged stool with one leg being the desire to pursue the truth,  the second the method used discern facts from fiction, and the last the power structure being the truth searchers.

The desire to pursue the truth is key to arriving at the truth. Without a public acknowledgement of that desire, there will be no guiding principle that these organizations can be held to by the public. This desire can bridge the gap between partisens in that it is not in support of this policy or that policy, but in support of what is true, what works and what doesn’t. Others will rely on these organizations conclusions as approximations of truth for the decisions that they make about their futures.

Before discussing a method to pursue the truth it should acknowledge that the term truth is a slippery subject. One person’s truth is another person’s lie. Yet I believe that there are judgements that can be made about topics of public concern (what are the causes the deficit, unemployment, or global warming) that use the scientific method to obtain the most likely explanation for such events. In my opinion, the scientific method is our best hope to sift through observations and conclusions made by everyone to arrive at an ongoing approximation of the truth.

Lastly, power relationships matter greatly in how observations and conclusions are weighed in pursuit of the truth.  Power relationships broadly defined means the amount of control one person has over another person’s needs. If a corporation that got rich through military contracting also owns a news outlet, there will be pressure to not cover negative stories about military contracting, or to downplay them if they come up. Career trajectories will be adjusted according to what stories are pursued and which stories are ignored. The people paying the salaries call the tune.

My Proposal

My proposal to meet the above requirements is to create an eco-system where organizations can form that embody those principles. The eco-system would be supported by software, a web site and perhaps native apps, that would embed within them the rules that enforce these values.

The first and most complicated aspect to address is the last leg of the stool, the balance of power relationships within and between these organizations and within society.  A key tool in maintaining a balance of power is to measure how power is distributed through the use of transparency. Who is getting paid and how much and by whom? Who is funding these organizations? Where are stories/news originating from and who is controlling their trajectory.  I am proposing a completely open structure including all accounting so that all flows of people, money, agreements will be seen by all and auditable and measurable by all.  People’s perspectives, and backgrounds will not be hidden. This will not be the view from nowhere. Every contributor to a story or event will have a public profile  stating their views and positions and a history of changes in personal positions and reasons for it. Transparency allows observers both inside and outside to see if and how power relationships are changing.

Secondly, it is important to encourage a very diverse membership in these organizations. Diversity should be pursued through many axes: gender, class, race, sexuality, geography, age, religion, political leanings, etc. Every person brings differing perspectives on what constitutes an important story to follow, highlight and research.  compromised in some fashion. In the pursuit of truth, many perspectives are needed to get down to what is true and what is not. For all of us there are issues for which we have assumptions about things that we have no direct experience with. We not only need to have diversity within the funding structure where people work, get paid and form the center of the organization, but also in the people that volunteer their time and knowledge in building and covering stories (through blogging, twitter and what ever other means are used to communicate throughout the organization’s eco-system).

The desire to pursue the truth through scientific and systematic inquiry are the two other legs of the stool and they give purpose and drive to the project and a way to satisfy that desire in a way that will yield results. The scientific method (ie. develop a hypothesis, test it through experimentation and observation, modify, rinse, repeat until the hypothesis fits the observations) has been the greatest driver of human knowledge in the past 500 years. It is our best hope in our fight for the truth and against ignorance.

So in final summary, I am proposing setting up a software eco-system to support the functioning of open transparent news gathering organizations in the pursuit of truth in a systematic and scientific manner.

This may not be a common pattern in rails applications, but a recent change in activerecord (somewhere between 2.3.5 and 2.3.11) code broke the following scenario:

class LineItem < ActiveRecord::Base   
  before_validation :build_visual_attributes   
  attr_writer :color, :height, :width # the components of visual attributes   
 
  serialize :visual_attributes   
  attr_accessible :quantity, :cost   
 
  def build_visual_attributes    
    write_attribute(:attributes, {:color => color, :height => height, :width => width})
  end
end
 
class Order < ActiveRecord::Base
  has_many :line_items
  accepts_nested_attributes_for :line_items
 
  attr_accessible :order_number
end
 
# LineItem #1 NOT updated
Order.update_attributres(:order_number => 123, :line_item_attributes => {:id => 1, :color => "blue", :height => "10in", :width => "20in"})

The change made it so that the line item #1 was not updated. If however a real attribute was included in the line_item_attributes, then it would be updated (as below).

# LineItem #1 is updated
Order.update_attributres(:order_number => 123, :line_item_attributes => {:id => 1, :color => "blue", :height => "10in", :width => "20in", :quantity => 10})

The reason is that when updating with a nested attribute, activerecord now checks to see if the record has actually changed. It does this in AutosaveAssociation#associated_records_to_validate_or_save in lib/activerecord/autosave_association.rb.

    # Returns the record for an association collection that should be validated
    # or saved. If +autosave+ is +false+ only new records will be returned,
    # unless the parent is/was a new record itself.
    def associated_records_to_validate_or_save(association, new_record, autosave)
      if new_record
        association
      elsif autosave
        association.target.select { |record| record.changed_for_autosave? }
      else
        association.target.select { |record| record.new_record? }
      end
    end

The old version of this method looks like this:

    # Returns the record for an association collection that should be validated
    # or saved. If +autosave+ is +false+ only new records will be returned,
    # unless the parent is/was a new record itself.
    def associated_records_to_validate_or_save(association, new_record, autosave)
      if new_record
        association
      elsif association.loaded?
        autosave ? association : association.select { |record| record.new_record? }
      else
        autosave ? association.target : association.target.select { |record| record.new_record? }
      end
    end

The old way returned the association if it was loaded or loads it if not. I am not sure why autosave would return true or not. But I know in this case it does return true. So in the new case, we now check to see if the record has changed using the following code:

    # Returns whether or not this record has been changed in any way (including whether
    # any of its nested autosave associations are likewise changed)
    def changed_for_autosave?
      new_record? || changed? || marked_for_destruction? || nested_records_changed_for_autosave?
    end

which in our case calls changed? in lib/activerecord/dirty.rb

    # Do any attributes have unsaved changes?
    #   person.changed? # => false
    #   person.name = 'bob'
    #   person.changed? # => true
    def changed?
      !changed_attributes.empty?
    end

And here is the crux of our problem. When we use attribute accessors to set the values from our form, our activerecord attributes don’t change. Since they don’t change, they aren’t saved and so the callbacks are never called which cause us to update the serialized attribute.

My solution to this issue, which may not be the best way, is to force a dummy value to be written to the attribute that we serialize to so that the callbacks get called so we can write the final value for this attribute. To accomplish this, I did a little meta programming to force that dummy attribute write anytime one of the values that feed into the serialized attribute gets written.

class LineItem < ActiveRecord::Base
  class << self   
    # serialized_attr_writer is used to allow writing to variables that are not active record attributes but are combined
    # into a serialized attribute for storage in the database
    # arguments:
    #   list of symbols for each instance variable to create a writer for -- just like the arguments to attr_writer
    #   :as - symbol for the name of the serialized attribute that combines the above symbols
    #   :value - a dummy value that the serialized attribute will be set to force the record to be dirty
    # 
    def serialized_attr_writer(*args)
      raise "last argument should be hash with keys of :as and :value" unless args.last.is_a?(Hash) && args.last[:as] && args.last[:value]
      options = args.last
      as_sym = options[:as]
      dummy_value = options[:value]
      args[0..-2].each do |arg|
        define_method(arg.to_s + "=") do |val| 
          instance_variable_set("@#{arg}", val)
          write_attribute(as_sym, dummy_value)
        end
      end
    end
  end
 
  before_validation :build_visual_attributes   
  serialized_attr_writer :color, :height, :width, :as => visual_attributes, :value => {} # the components of visual attributes   
 
  serialize :visual_attributes   
  attr_accessible :quantity, :cost   
 
  def build_visual_attributes    
    write_attribute(:attributes, {:color => color, :height => height, :width => width})
  end
end

The original commit that caused this issue to appear fixed this issue