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"

Git workflow and my new love of Git alias

For good or bad, my current Git workflow does not include merges, but rebasing.

It keeps the history cleaner, works with the established workflow (pretty standard GitHub flow) just no merge commits.  I was trying to find a nice graph of the master branch, but coming from a GitLab background it isn’t hard to find this information in the GitLab repo, but I don’t think GitHub really has this history feature. Anyone know the best way figuring out the history on GitHub?

We really enforce making sure to have meaningful commits, and commit messages. When you are working on your local branch, feel free to commit away, but an interactive rebase is needed before requesting your pull request. After you have done a couple of these rebases, there really isn’t anything to be afraid of. I hadn’t done one, and let me tell you, my first pull request was pretty painful. I think getting through the review process on that, and getting my commit history clean took just as long as coding my feature >_<. Now me, rebases, and amends are friends.  I haven’t had a chance to use fixup commits yet, but they are something on my raider to try out soon!

I enjoy using Git bash for my source repository needs.  Even with my more advanced workflow at my company, for me it has the most flexibility.  I have given Sourcetree (works better with BitBucket then GitHub) and Git Kracken a try before, and it is nice to have visuals when you need to deal with complex conflict situations, but I keep on going back to my trusty Git Bash.  I never used to be a command line person, just in certain situations, but now especially since I have switched over to Ruby and Rails I have at least 3-4 terminals open at anyone time. I even exited Vim yesterday without having to look it up! I feel that should be some sort of milestone.

 

 

 

 

 

 

Also I have been making use of GIT CLI alias, also installed multiple scripts to run in the terminal (terminal parrot I’m looking at you!).  Some of these help me with my job more then others :). If you don’t have a git alias, get yourself one stat. You just need to add lines to ~/.gitconfig

[alias]
    st = status
    ci = commit -v

Some of my personal favorites right now are:

st = status
co = checkout
dm = diff –patience master
prb = pull –rebase
pu = pull –rebase origin master
pf = push –force-with-lease
po = push origin master

What’s in your Git alias? And also how awesome is Command+Option+Shift+V on MacOS? That was the key to my problem with copy and pasting text and not keeping the old formatting!

The danger of Slack

Slack is a wonderful communication tool, I love how easy it is to get in touch with my co-workers, but it is also a huge time suck which I have become slightly addicted to.  It is way to easy to start browsing through a channel, start checking on the pinned items (#living-vicariously, I’m looking at you), and then look up and realize that you have lost a hour of the prime working hours. When I first started my current position, I was so excited to have Slack.  It was absolutely verboden at my previous workplace, and my one experience was using it at a hackathon just for about 24 hours. It was so shiny, and packed to the brim with emoji’s, gifs and fun times.  Share your screen! Message! Share! For a while, I even had my laptop screen open with just Slack on that screen.  You only have so much attention and concentration to spread around, and my brain power was getting sucked into Slack channels.  I even had a couple weird Slack related dreams, and that is when you know you are spending waaaaaay to much time on it. It even felt a bit Circle to me at times, I related to a lot of parts of that book.

Now my getting down to business routine involves fully closing slack, muting other notifications, closing all unneeded chrome taps, and then putting in the headphones.  How do you focus? Is Slack a time suck for others or is it just me. Might just be my fear of missing out (FOMO) coming into play here too ¯\_(ツ)_/¯

Taking a step back from tactical to focus on strategy

We just got through a full company retreat.  Thinking back over my career, this might be the first company that I worked at that actually did one of those.  I have down team things before, and had all company meetings, but not a full on retreat. It was great to see the entire company come together to work on a common theme, and stop our day to day work to take a step back and look at our upcoming focus from a higher level.  Sometimes you get stuck in the weeds and focus on getting stuff done, you don’t think about the why.

There were some great talks by my fellow employees (the employees were in charge of the programming for one of the days), and one that stuck with me, discussed effective vs. efficient and how one should work first on the effective and then try for it to be efficient. I see this all the time pop up with processes that occur around requesting code changes, deployment strategies or how the business works with IT.  It also made me think back to my first software engineer position, I was an associate at a transaction processing software company. For my first code review I actually printed off my code. Literally onto paper, handed out copies to my product group and they made suggestions, in pen, for the code.  Was it efficient? Not on your life, but for some reason that was how it was done and that was how I was supposed to do it. This might have been somewhat effective, as a learning experience and sharing what people were working on, but was not at all efficient.

When you just focus on your day to day, and getting work done these type of problems just seem to pop up.  They usually start because it was the easiest or quickest way of fixing something. If we don’t take a step back and look at the why, and our long term goals, it is easy to get bogged down in the weeds.

 

 

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.

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.

 

Music for learning

So picture this, you are sitting down at your desk, loaded up your tutorial, terminal, editor and have muted all your slack channels, you are ready to go.  But now you have to figure out something to listen to so you can tune out everyone in your open office, but what should you load up?

For me, I need something with no lyrics.  I have a hard time concentrating and really being able to focus when I hear other words.  So sometimes browsing on Amazon for a new playlist can be frustrating. EDM really is hit or miss, things that you don’t thing should have vocals end up having them.  There needs to be a no-lyrics tag, some way to make focus playlists more searchable.  Right now my go-to is Tycho, really anything by him.  Although, today I was trying to listen to the Past is Prologue album but it kept on making me think that my streaming service was encountering some lag or other glitch, maybe this is just me? I also am a fan of Brian Eno, but sometimes you just want something more uptempo.

Any suggestions for good focus music?

Edit: Here’s a Spotify playlist I have started with some suggestions, and my favs.

Follow-up (7/24/18): I forgot to mention Bolling: Suite for Cello and Jazz Piano Trio and Deus Ex soundtracks. Both have been in rotation lately, along with my all time favorite 2112 by Rush, and the Spirited Away soundtrack by Joe Hisaishi.

Constructive procrastination or how to create your very own slack emoji

I learned new techniques and tools today, but they definitely aren’t very relevant for my job but they were fun to work on.  I just animated my first slack emoji today, and learned a ton in the process.  If you too would like to be the rockstar on your slack channel, buckle in and I will teach you how to do your own emoji.  First off you will need a photo editor, because I didn’t want to buy anything and it is an awesome piece of software, I went with GIMP.

For slack, the best emoji size is 128×128 pixels.  So open a new image that is 128×128, next you can add a background image or solid color.  I created a squee emoji that had scrolling text in it, so I went with a white background with scrolling magenta text. As you can see, this was a huge gap in my companies custom emojis’ and needed to remedied asap.

From there decide what you want it to do.  I went with scrolling because that seemed easy.  Each of your layers is a different part of animation.  So with scrolling, each layer moves it slightly to the left.  You could do confetti or colors and then each of the layers is the movement.  To check what it looks like, go to Filters > Animation > Playback … This will show you what your animation will be.  Once you are happy with the look then use Filter > Animation > Optimize (for GIF) and then export as a GIF image.  For more detailed instruction check out this awesome tutorial from GIMP.org . Now you are ready to upload it and use on slack, if you need some help in that department, check out Slack’s help guide.

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.