Rails on Maui

Programming in Paradise

Success the ShakaCode Way!

Republished from www.shakacode.com

It’s Thanksgiving today! I’m taking a few minutes to put down a few thoughts that I hope will bring you, your family, and your friends “success” in the coming year.

Success is ambiguous. Is it being a millionaire? Being famous? Winning competitions?

I’d say it’s happiness! This image captures part of my definition of success. Jimmie Hepp took this picture just a couple days before I wrote this article.

Note, I said happiness is just a part of success. Balance in life is key to success. While I have the means to spend all my time surfing, I’d find greater happiness in striking a balance with my other passion, which is creating great software and an organization called “ShakaCode”. ShakaCode is more than just a company; it’s a community. A large part of what ShakaCode does is giving away free code and training to world’s software community, as shown by our open source projects, our ShakaCode forum, and my RailsOnMaui blog.

A big part of the DNA of ShakaCode is that we’re remote first. This means that you can be involved with ShakaCode regardless of just about any factor other than having a decent computer, good Internet, and a true passion to learn and contribute. And if you have family duties, just hate time wasted commuting, or want to live at a ski resort or a remote surf break – that’s all OK. Educational background, ethnicity, nationality, gender, age, etc. all do not matter to us. All of our team members share this philosophy and this is how we find new team members.

Who Is ShakaCode?

Republished from www.shakacode.com

This gesture is more than just a mere wave or thumbs up. The shaka is a symbol of the “Aloha spirit,” which is the coordination of the mind and spirit to think and exude good feelings to others. (by Megan Denny, www.padi.com)

by Justin Gordon with help from the ShakaCode Team, September, 2015

Who is ShakaCode?

It’s the global web development software consultancy and product company started by Justin Gordon, AKA “railsonmaui” in the Ruby on Rails world. We’re focused on what we believe to be the best technologies for web development. Today, those are Ruby on Rails and React. But it’s not just us, we like to think that it’s the larger WE in the open source community that loves collaborating on beautiful code.

Masking PII With Ruby Gsub With Regular Expression Named Match Groups, Non-Greedy

In this article, you’ll learn:

  1. How to effectively use rubular.com and the Ruby console to get the correct regular expression syntax.
  2. What is the difference between .* and .*?, greedy and non-greedy.
  3. What are regular expression named capture groups and why you should use them.
  4. How to use String#gsub without and with the block syntax, and without or with named capture groups.

Suppose you have to filter out PII (Personally Identifiable Information) out of log entries that look like this HTML. We don’t want the following PII fields to show their values: email, social_security_number, date_of_birth

Input Html

1
2
3
4
5
6
7
8
9
10
11
Updated User (4)<br>
     changed first_name to &quot;Karina&quot;<br>
     changed last_name to &quot;Senger&quot;<br>
     changed phone to &quot;2133432154&quot;<br>
     changed email to &quot;brenna.treutel@runolfsdottirdonnelly.org&quot;<br>
     changed street to &quot;123 Main St&quot;<br>
     changed city to &quot;Paia&quot;<br>
     changed state to &quot;HI&quot;<br>
     changed zip_code to &quot;96677&quot;<br>
     changed social_security_number to &quot;555-33-4444&quot;<br>
     changed date_of_birth to &quot;2000-10-03&quot;

And the end result we want is:

1
2
3
4
5
6
7
8
9
10
11
Updated User (4)<br>
     changed first_name to &quot;Karina&quot;<br>
     changed last_name to &quot;Senger&quot;<br>
     changed phone to &quot;2133432154&quot;<br>
     changed email to XXXXXX<br>
     changed street to &quot;123 Main St&quot;<br>
     changed city to &quot;Paia&quot;<br>
     changed state to &quot;HI&quot;<br>
     changed zip_code to &quot;96677&quot;<br>
     changed social_security_number to XXXXXX<br>
     changed date_of_birth to XXXXXX

Enums and Queries in Rails 4.1, and Understanding Ruby

Sometimes when you get puzzled by what Rails is doing, you really just need to understand what Ruby is doing.

For example, given this simple code to get an attribute value:

1
2
3
4
# return value of some_attribute and foobar
def some_attribute_foobar
  "#{some_attribute} and foobar"
end

Beginners are often stumped by why this code does not set an attribute value:

1
2
3
4
5
6
# change the value of some_attribute to foobar
def change_some_attribute
  # why doesn't the next line set the some_attribute value to "foobar"?
  some_attribute = "foobar"
  save!
end

What’s going on?

In the first method, some_attribute is actually a method call which gets the attribute value of the record. This works in Rails ActiveRecord due to the Ruby feature of method_missing which allows some code to run when a method is called that does not exist.

In the second method, a local variable called some_attribute is getting assigned. There is no call to method_missing, as this is a variable assignment!

The correct code should have been:

1
2
3
4
5
# change the value of some_attribute to foobar
def change_some_attribute
  self.some_attribute = "foobar"
  save!
end

Adding a JS LIbrary to a Ruby on Rails Project When Using Webpack

What’s it like to add a JavaScript library when Webpack is integrated into your Ruby on Rails environment, per my article: Fast Rich Client Rails Development With Webpack and the ES6 Transpiler?

It’s super easy! But what if you want some of your the legacy JavaScript or CoffeeScript code in your Rails app to access the Webpack added library?

Here’s a real world example. Suppose you want your JavaScript code to round numbers to decimal places. Math.round() only rounds decimal numbers to the nearest integer. A code sample on that page is provided to show you how round numbers off to some number of decimal places.

A quick Google for JavaScript libraries finds npm package compute-roundn. A look at the github repository for compute-io/roundn reveals clean code and it has some tests.