Wednesday, December 10, 2014

7L7W Week 1: Ruby Day 2>Take 2

Reading through the 3rd day's treatment and I decided to go back over this exercise from day 2.
This is better. I'm still not entirely convinced.
class Hash
  def each_hash
    h = self
    h.each do |element|
      yield({element[0] => element[1]})
    end
  end
end
class Tree
  attr_accessor :children, :node_name

  def initialize(name, children=[])
    if name.is_a?(String)
      @node_name = name
    elsif name.is_a?(Hash)
      @node_name = name.keys[0]
      name[@node_name].each_hash {|trunk|
        children << Tree.new(trunk)
        }
    end
    @children = children
  end
  
  def visit_all(&block)
    visit &block
    children.each {|c| c.visit_all &block}
  end
  
  def visit(&block)
    block.call self
  end
end

familyTree = Tree.new({'grandpa' => {'dad' => {'child 1' => {}, 'child 2' => {}}, 'uncle' => {'child 3' => {}, 'child 4' => {}}}})
puts "Visiting a node"
familyTree.visit {|node| puts node.node_name}
puts

puts "Visiting entire tree"
familyTree.visit_all {|node| puts node.node_name}


The notion of editing the base class at will and indeed the basic predefined classes/types, while apparently being an integral part of Ruby's design, doesn't come easily. And still this solution requires testing the type of the incoming parameter, making Ruby's blasé treatment of types rather dubious. Or, I'm doing it wrong™ which is also quite possible seeing as this is exactly the 3rd day I've done anything in Ruby.

What I've done here is basically the same as a C# extension method. What gives me the 'icks' is the notion of directly altering Hash.each to do what I wanted it to do. It seems that you can't scope the changes so it's a global edit of the existing class. Great power, &etc, plus TDD, but if anyone comes along later to update the script and tries to do anything with a hash while those changes are in effect it's going to bite them in the tail.

I think I'm feeling a tension between reducing failures and productivity at the cost of stability. It's hard to decide where the sweet spot is there. That's interesting since I spent a good bit of time developing in PHP which is pretty loosely typed.

Tuesday, December 9, 2014

7L7W Week 1: Ruby Day 2>A spoonful of sugar

I feel like I must be missing something. Because this:
class Tree
  attr_accessor :children, :node_name
  def initialize(name, children=[])
    if children != []
      @node_name = name
    elsif name.is_a?(Hash)
      @node_name = name.keys[0]
      name[@node_name].each {|trunk|
        children << Tree.new(trunk)
        }
    elsif name.is_a?(Array)
      @node_name = name[0]
      name[1].each {|trunk|
        children << Tree.new(trunk)
        }
    end
    @children = children
  end
  
  def visit_all(&block)
    visit &block
    children.each {|c| c.visit_all &block}
  end
  
  def visit(&block)
    block.call self
  end
end

looks ridiculous. The main problem seems to be that name[@node_name].each is giving me arrays instead of hashes. Which means I have to account for another type that I can't readily predict in advance... which seems to end up being the bane of dynamically typed languages. Yesyesyes ducktyping, &etc. I can't access elements in a hash the same way I access elements in an array so that's not really applicable here.

On the other hand,
i=0
File.open("data.txt", 'r') do |file|
  file.each do |line|
    i += 1
    puts "#{i} #{line}" if /dialog/ =~ line
  end
end
puts "----------------------------"
puts "Searched #{i} lines of file."

was relatively easy. Except for the false start with File.foreach which didn't seem to actually be giving me full lines from the file. Looked like it was giving me strings the size of some internal read buffer. Frankly, I expect enumerating a file to give me each line. Maybe it was user error.

Still. I'm not particularly impressed with Ruby. It feels a lot like working in PHP 8 years ago. Only with anonymous functions. Day3 is subtitled 'Serious Change' so maybe that'll be interesting.

Monday, December 8, 2014

7L7W Week 1: Ruby Day 1> Hellooooo Ruby

Somehow I was expecting more... wow factor? For as popular as Ruby is and for the kudos it gets it really doesn't, yet, feel more natural or seamless to me. Maybe I've spent too much time with 'harder' or 'crustier' or 'more obtuse' languages to get it. /shrug

Maybe day 2 will have more excitement; I will endeavor to be open minded about it until at least day 5.

Seven Languages in Seven Weeks

Go.

Wednesday, December 3, 2014

Chrome autocomplete

I am amused that I can type 'gith' to go to github.com.
Is Chrome making a statement here? >.>

Monday, November 17, 2014

The Value of Repeating Yourself

Brian Douglas posted a great comment on his study practices. Worth a look at Concentric Circles in Learning and, anyway, what follows is a continuation on the theme so you might as well take a peek.

My personal take on the notion is to do the example project alongside the book you're studying. After the first run through create a new project in a different topic. It doesn't have to align precisely with the 'canon' project and in fact if it's subtly different that's actually better. Now you track through the book using it as a scaffold to build out your app.

The goal is for your second run through the material to be more about translating what's in the book to what's in your dev project; you want to be constantly thinking "oh, the book says fooDbEntry but in this app the object that handles the database entry I want to change is barDbEntry". It forces you to better understand what's going on under the code you're writing, how your chosen task differs from the example, and more importantly how you have to adjust the example code to meet your new goal. That directly builds and reinforces your mastery of the underlying concepts and trains your mind to see things through the code instead of through the domain you're writing the code in.

Of course there's the additional benefit of having built something (possibly) useful that you might want to elaborate on instead of yet another example app which is rarely anything that anyone wants to revisit. As a development process it's ridiculously inefficient however the goal isn't to use this as a development process; it's a learning process.

This is, perhaps, less applicable to course material like you might find on a MOOC or similar online location where you're heavily railroaded and it's difficult to flip around the material from place to place. Just an example of how the printed word (paper or screen) is always valuable even alongside an audio or video format.

Monday, November 10, 2014

Visual Studio published my DB passwords

Wasn't that nice of it? *.pubxml is .gitignore-ed innit? Why yes it is. VS ignored that directive and published it anyway. Thanks much. Fortunately it's just a low profile Azure SQL DB and a learning experience in resetting the password on the Azure DB plus fixing the password configured in the relevant Azure website.

More importantly, google found me this:
http://ericnelson.wordpress.com/2014/06/21/is-visual-studio-2013-ignoring-your-gitignore-file/

Which worked like a charm. Cleaning up after the problem took three times as long as actually fixing it. Sounds just about right to me.