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"

Exploring Ruby IDE Options

I have been working in Ruby and Rails a couple weeks now, but I don’t know if I have been entirely happy with any of the editors/IDE that I have worked with.  Coming from a Java background I used Eclipse/Spring Tool Suite for all my development work.  I knew the short cuts, how to browse quickly and easily, and nothing with Ruby yet has felt like it has clicked.

I have spent the most time in VS Code right now, but I keep on finding features that I am yearning for.  Like local code history, code templates, different views… I have been trying out RubyMine for the better part of a week, and it almost feels like too much going on.  I haven’t gotten a good hold on customizing it, and for some reason when I try the hot keys, they never seem to work.  I know I could change up the short-cuts, but that almost seems like cheating to me, it really isn’t learning the new IDE way of doing something but attempting it to be like something it isn’t.

The lead developer I have been pairing with this week is an emacs wizard.  He makes it look effortless and insanely useful.  I think I might have to give that a go, but if it is anything like Vim I know the learning curve will be steep.  If I remember how to exit properly in Vim, it feels like a win. At home on my windows machine I’m just using powershell for a terminal and Atom for a editor.  It’s super simple, and works great for the small coding exercises that I am doing, but would not work in an enterprise level application.

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.

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.

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!