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.

Wednesday, October 29, 2014

Creating a custom snippet in Visual Studio 2013

Bleh. Checklist-simple procedure that I'm tired of re-researching.

First, reference material from MSDN

Altering an Existing Snippet

If you want to alter an existing snippet to make it line up better with your workflow:

  1. Ctrl+K, Ctrl+B (or Tools >Code Snippets Manager)
  2. Find the target snippet
  3. Highlight the "Location:" field above and copy to clipboard.
  4. Open >File...
  5. Paste the snippet location
  6. File >Save (filename) as...
  7. Browse to your snippet repo (you do have someplace safe to store these as 'source' documents right? these are productivity enablers after all)
  8. Name it appropriately (Custom usually works well)
  9. Proceed to the next section.

Build Your Own

If you're creating something from (mostly) whole cloth... well, don't reinvent the wheel. Follow the steps above but for the target select an existing snippet that does the same kind of thing you want (scaffolding with fill-in templating like the 'if' snippet, or pure expansion, or whatever). That way you have all the details already written and existing syntax to build from. XML documents can be particular and there's no reason to waste your time learning all the special syntax for code snippets. You've more productive things to do than become a productivity tool expert.

Editing

A few words on editing your custom snippet XML. 
Header is where you'll put metadata like author, description, etc. Make sure these accurately reflect what you're doing, and don't try to blame "Microsoft Corporation" for your hacked-together XML drivel.
Double check sections like and because you don't want your snippet to accidentally import libraries that the final code won't require (the 'testm' snippet for example references some unit test libraries that won't be required if you aren't creating unit test code)
Declarations is where you'll set up user input fields and IDE-ready type references (testm is a good snippet to see how these work)
Indents are quite tricky in my experience. They seem to be literal with the first line beginning at the start or the shortcut text in the editor. You'll probably have to play around with it (see importing below). I can't seem to get it to properly indent based on the indent level the shortcut begins at. However it works properly for the built-in 'if' snippet so clearly it's possible. It must be a personal failing.

Importing

  1. Open the snippet manager again (Ctrl+K, Ctrl+B)
  2. Click Import...
  3. Browse to the XML file and select it.
  4. Pick a folder to drop it in. May I suggest the pre-existing "My Code Snippets" folder?
  5. Hit OK and OK.
  6. Test it. Inevitably it's broken in some way. Identify the problem (google can be your friend but more likely for something this esoteric the MSDN doc above will be more useful).
  7. Make the necessary edits and re-import. When you're asked how to handle the conflict just overwrite.
I thought the last time I did this there was a way to edit the snippet in place but I've forgotten how I did that. Possibly by opening the snippet in something other than VS (it's pure XML so most XML editing programs should handle it just fine) but you might have to re-open VS before the changes take effect. Frankly I'm not going to devote any more time to it right now; I'll update the post if I learn better later.