Category Archives: Uncategorized

Check Array Contains only Blank Strings in Ruby

This is a nice trick that I recently learned. So, this is a part of an app integration with Google Sheet. The array is an interpretation of a row. The app should check if all the values inside the row is valid, but should skip if the row is empty. Now sometimes people left some space unintentionally inside some cells. These cells technically are not empty, but I wish the app is smart enough to detect this and mark the row as empty.

For example:

my_array = ["    ", "  ", "", "       "]

I have to detect my_array as an array of blank string

So here’s the check that I use:

my_array.map { |e| e.gsub(/[[:space:]]/, "") }.uniq == [""]

UPDATE:
Here’s another check that use Rails, based on some input that I got (thanks guys)

my_array.select(&:presence).empty?

my_array is the array that you want to check. gsub will deletes all spaces inside string element of the array. then uniq equality will check if there is any non empty string left.

Ok, hopefully this trick can be useful for other people. Cheers!

Be Careful with Not

One thing that I like with ruby is it have a lot of English sounding keywords: not, and, or, unless, etc. However, you should be aware that these keyword might not be equivalent with their “traditional” counterpart.

Take `not` for example. We use `not` to negate a boolean, which we traditionally use bang (!) for. However, not has different precedence level from `!`. In ruby, `!` has the highest precedence, while not has one of the lowest precedence, like you can see in the ruby docs. This might have surprising effect.

!true && false
=> false
 
not true && false
=> true

In the first example, ! has higher precedence than `&&`, so it will evaluates as `(!true) && false`. However in the second example `not` has lower precedence than `&&`, so it will evaluates as `not(true && false)`.

Another example

a = true
b = not a
SyntaxError
 
b = !a
=> true

Because of this subtle difference, I avoid using not and just use ! every time.

Sources:
https://github.com/bbatsov/ruby-style-guide#bang-not-not
http://stackoverflow.com/questions/27429711/can-someone-explain-not-vs-in-ruby

Role models are important

There are things that I wish I knew earlier when I just started learning to code. One of them is the existence of static code analyzer / linter. Using them will teach us about many style guide that established company use and make our beginnerish code better.

I learn a lot by copying, including copying other people style. Now of course as you gained more experience and understanding you might develop some preference that deviates from other’s. But it is useful to know where the norm are.

In Ruby, the de facto one tool to do this is Rubocop. I found out about Rubocop more than one year after I start learning Ruby. When I run it againts my old codes, I see tons of “offense” hahah. I think more people should know about rubocop earlier. That’s why, I did a talk about it in id-ruby community!
id-ruby-talk

Here’s the slides if you want to look.

Now that I am starting to learn Javascript seriously, I will not make the same mistake. I look around for Javascript’s Rubocop and I found eslint. Let’start coding and revising!

 

  • I steal the title from Rubocop, which is a quote from Robocop

 

Time to Start Giving Back

Recently, I realized something.

This is my github profile if viewed when I logged in:

github private

and this is if viewed when logged out

github logged out

What do they tell? They mean that my contribution to private repository totally dominates my programming life. It is even worse actually, since some of my projects were done in bitbucket.

Now, web development is my bread and butter, so of course my for-profit project will dominate. But I have benefit so much from the open source community, my career actually depend on it, that not giving back is not an option.

So, now I intend to consciously spend more effort to contribute to open source repository.  I have started some:

  1. This is my first PR ever that has been merged in to other people’s repository, only two days ago:
    https://github.com/thoughtbot/fake_stripe/pull/54#event-763737947
  2. I started an open source project. This basically for me learning about nokogiri and rails-api, but perhaps other people might find it useful
    https://github.com/waruboy/scraperry

Ok, if you think you have benefited from the open source community, please consider to give back. No contribution is too small. And it is not only by submitting to open source repository, some other ways, like writing a free access blog post, help too!

 

 

Books: Agile Web Development with Rails 4

I just finished this book from The Pragmatic Bookshelf. Agile Web Development with Rails 4

agile web development with rails

The book is divided into three major part. The first part is an introductory part about ruby, how to install Rails, etc. The second part discussed about how to build an online catalog using Rails. You can check out the online catalog that I built from this book here. I wish the look is not so bland, but anyway it does what it is supposed to do. The last part discussed many Rails topics in-depth, such as Active Records, Rails dependencies, etc.

If you have finished your first introductory materials to Rails and looking for more learning materials, I can recommend this book for you.

Skip bundle exec when using rbenv

If you are using rbenv and bundler like I did, you must be feeling tired writing ‘bundle exec blabla’ everytime you use executables in your rails project. Fortunately, you don’t have to. You can use this rbenv plugin to automatically bundle exec your command. By default this plugin ignore ‘gem’ command, since normally you shouldn’t run gem with bundle exec.

While we are at it, another useful rbenv plugin is rbenv-gem-rehash. That plugin automatically run ‘rbenv rehash’ everytime you install a new gem. So, you don’t need to rehash everytime you run ‘bundle install’

This is a short post, but hopefully it’s usefull enough. See you!