Introduction to OpenStruct

Intro to OpenStruct

Last week I had a task to start working with some CSV data pulled in from the database.  There was already existing code working with the data, so I needed to make sure whatever changes were applied, that they were backwards compatible.  The data was an csv representation of an array.  The current code was hard coded to go through the array in a fairly rigid process, so to add some flexibility, and allow better access the data in a controller, it was suggested to use OpenStruct.

First a quick overview on what is OpenStruct, it is a data structure that is similar to a hashe. It applies arbitrary attributes with accompanying values. It seems like magic, but it is just Ruby’s metaprogramming and it defines the methods on the classes themselves. A basic example of using OpenStruct on an object might be:

require "ostruct"

candy = OpenStruct.new
candy.name = "Snickers"
candy.type  = "Chocolate"
candy.amount = 5

Then if I needed to call and figure out the candy name, or type, I can call a method on the object itself (instead of having to do the Ruby hash way which would’ve been candy[:name] # => “Snickers”)

candy.name
> "Snickers"
candy.type
> "Chocolate"

So the code went from using:

@csv = CSV.parse(parsed_text)

To the following with OpenStruct:

@csv = CSV.parse(parsed_text, headers: true).map { |row| OpenStruct.new(row.to_h) }

Making use of the csv data now went from:

@list = @csv[1..-1] || []
@count = @list.count

And now:

@list = @csv.map { |row| row.to_h.values } || []
@count = @csv.count

Accessing the values was much easier, but it is important to note with OpenStruct:

  1. It is not performant, which wasn’t something that needed to be kept in mind for this piece of code but if it is something that you do need to keep in mind, go for a hash or struct.
  2. OpenStruct != Struct , struct is similar to a custom created class that you don’t need to initialize or have any attr for. Struct is more performant, OpenStruct more flexible.  Struct has to have the attributes defined, while it is dynamic with OpenStruct.  For the ex. we have name, type and amount but couldn’t add on later an listing for populatarity.
candy = Struct.new(:name, :type, :amount)
snickers = candy.new("Snickers", "Chocolate", 5)

snickers.type 
> "Snickers"

Update on Ruby and Rails learning experience

Even though I may not be writing about it, I am continuing my deep-dive into the word of Ruby.  At work I am mostly focused on Rails.  My app is Rails, and the tickets I am working on are Rails (with some haml and scss thrown in, yay for learning some front-end development!).   Currently I have been able to be become semi-productive, but haven’t  been able to complete a ticket on my own, which is a humbling experience.  We are doing one week sprints, so I do try to muddle my way through on an assigned ticket.  But since time is short, you need to just women up and ask for help sooner then later.  When time allows, I am still working through a Rails Tutorial.

At night, I have still been working through Ruby the Hard Way and have made it up to exercise 15 or so.  These have been very easy so far, but I have a feeling that it will pick up speed soon and I don’t want to skip something that might be important. Got derailed this week because one of the lessons is working on accepting user input, and suggested looking up Zork, so the rest of my evening was playing that. It kinda reminds me of Vim, if you can remember the keywords it can be fun, but if not you are just kinda stuck in a dark room (terminal).

In other fun news, I actually have people visiting my website! Um thanks, I really didn’t know that anyone would read this, and with my handy dashboard on Flyweel I can see that I have visitors. Now what percentage of those are spam bots, who knows  ¯\_(ツ)_/¯

Side note: if anyone knows why when I run my rails server I get “MiniProfiler storage failure: Connection lost (ECONNRESET)” every once in awhile, could they share, that would be awesome.  Everything starts up and runs, but I just see that pop up pretty often.

Finding out how little you know about Git

I haven’t been a Git user for a long time, I have just been actively using it since January. That may not sound like a long time, but for a developer using a tool for over 5 months is almost as much as some projects take (if you are lucky) or as long as a contract is.  Since switching from Java to Ruby, and moving to a new company who knows much more about this, I have learned how unsophisticated we were using Git.

We were on GitLab, using it with bash in a modified GitHub flow with a master and develop branches, all features were branched off of the develop.  We did have merge commit messages, and had some CI/CD integration pulling in from a internet accessible GitLab to an internal GitLab.  The switch from Subversion was done fairly easily, with some reorganization needed to switch up some of the repo structure (we went for a one-to-one relationship with all project == new git repo, but that made waaaaaay too many repos).  So, I had been doing some Git, and now I have learned that I really don’t know how to do Git.

Did I know what a Git alias was? Nope. What about rebase, pushing with options, or forking a repo? Nope, nope and nope.

So I now I have a git alias (that I have to have pulled up in a notepad so I can remember all the commands) and have to be a lot more specific with my commit messages (they are a-ok for development but when you are ready for you pull request, squash those babies) and no more straight pull and pushing for me! I need to remember to rebase, rebase, rebase and for the love of all that is good don’t add in extraneous commit messages, make sure to amend. With all this changing of history I am doing, I am feeling like a time-traveler.

 

The problem with starting

Sometimes there is just so much information and resources that making the first step takes a long time to decide, just because you don’t where to start.  There are so, so, so many free amazing resources for learning ruby that I keep on getting distracted jumping from one to the other.  Today did not feel like the best learning day. I didn’t get very far on my Ruby Koans, I didn’t get much other reading done (although a co-worker shared a great article on the power of small wins that I am adding to the pile).

I need to just pick one and go with it. Going to stick with Ruby Koans, because I already started it and I feel like right now I just need to finish something.  At home I am working through Learn Ruby the Hard Way, and that is it for my resources for now.  Besides starting in on application code for work, I am going to try and limit my Ruby resource for a while because frankly I am overloaded on information right now.

A new day, a new wordpress theme

I’m not only new to ruby, macOS, but also to WordPress. Since at my new workplace, WordPress is kinda a big thing, I knew that I needed to learn this too. I am still trying to get a handle on themes and how to edit the look and feel of the website. I want to keep it clean, really nothing too fancy is needed but a bonus would be if it actually looked good too. So here’s to using Flywheel’s awesome WordPress hosting, but also being a member of the dashboard team.’

Also started up working through Learn Ruby the Hard Way yesterday evening. So far so good, only issue might be that my home computer is a WindowsOS, and my work is a MacOS. I don’t think it is bad that I learn PowerShell, but probably won’t help my day to day.

Ruby Koans – Day 1 Progress

Slow start at first, but I think I am picking up speed.  equals and == sucks, they mean the exact opposite that they do in Java.  Ruby, == is equivalence (.equals in Java), and use .equal? to check if the objects are the same (== in Java).  That’ll probably hang me up for a while.  This resource from ruby-lang helped out, it gave me a quick context to wrap my mind around.

11/282 Koans finished end of day

Haven’t figure out my first Ruby book to try, but I will probably be giving this Ruby in 20 Minutes tutorial a try, and Ruby Monk’s Primer a go too.

 

Ruby Koans – Is this the path to Ruby enlightenment

Ok going forward I am trying out iTerm for my terminal and Visual Studio Code for my editor. Don’t know if these are best practices or anything, but when I was trying to pull down code from GitHub in just the default MacOS Terminal, it felt clunky.  So I am running through the Ruby Koans tutorial because it seemed more complete to me.  I am a programmer, I just am not a Ruby programmer.  So I know that I want to look at code structure, not just a dinky hello world app that doesn’t give me any real world code feels.  So I am going to run through these and see what I learn.  Also pulled down a couple books and might start in on one of those, but for right now I just need to get my hands on some code that I can read.

 

I’m starting a Ruby blog

It’s 2018 so it seems like a good time to start a blog.  That’s a thing still right? It must be if my default wordpress theme has it. So I find myself starting a new career. I did this to myself, so I only really have me to blame.  I am or I was a Java engineer.  It was what I did for a living, and kind of my identity. So it was what I did for 13 years, and my god that sounds like a long time now that I am typing it out. Now that it is not the case, I signed myself up for a new career at Flywheel (not the sports one) and I have totally flipped to a Ruby stack.  To add more to my plate, I have also switched over to a Apple OS from Windows, and a start-up over a large enterprise operations.  Pretty much, it is different then everything I have done up until now.  Please wish me good luck!