Week 45

Running rubocop on changed files Really nice article describing about how to use rubicon when developing new features

# before committing
git ls-files -m | xargs ls -1 2>/dev/null
# before pushing
git diff-tree -r --no-commit-id --name-only master@\{u\} head | xargs ls -1 2>/dev/null<span 				data-mce-type="bookmark" 				id="mce_SELREST_start" 				data-mce-style="overflow:hidden;line-height:0" 				style="overflow:hidden;line-height:0" 			></span>
Advertisements

Week 30

I didn’t realize how fast time flies….

Sinatra

Sinatra explained by Zhengjia: great tutorial, dive deep into how Sinatra works.

Let’s Build a Sinatra – by Gabe Berke-Williams He is showing how to build a Nancy – a similar type of Sinatra – from scratch. I can now understand how to build the Classic type

Almost Sinatra in 6 lines of code

How to enable sessions with Sinatra: overview of the defaul Sinatra session, then Rack::Session::Cookie, and the last Rack::Session::Pool

Delegator

To provide syntatically sugar for Sinatra Classic style

TIL

diff-so-fancy

Thanks to Sergey, I know about diff-so-fancy

Inheritance vs. Composition

https://www.thoughtworks.com/insights/blog/composition-vs-inheritance-how-choose

UX, Usability

UX toolkits – from Thoughtworks

Codrops playground: open-source CSS & Javascript for many interactive website, from Mary Lou. It’s such a fantastic work.

10 usability heuristics – from Nielson Norman Group

Usability applied to life – for fun

 

 

TIL

git clean

The documentation for git clean is clear, but still it’s long. So here comes the summary

git clean -n # Dry run, show list of untracked files to be deleted
git clean -n -x # Dry run, show list of untracked + ignored files to be deleted

git clean -f # Remove untracked files

git clean -x -f # Remove untracked + ignored files

The order of middlewares matter

Despite having working with Ruby for more than 6 months, I have never really understood the usage of middlewares inside a Rack application.

We are working on a problem to set the X-Request-Id header for the incoming request, and we would like to have that header in the response as well.

So for setting the header, we use a middleware layer, named RequestId. Initially I thought we would need another middleware to set a header for a response as well, but no, we do not need it.

A request is coming to Rack app with its set of headers. Then whenever we modify the headers for the Rack app

This is the sample response from Rack app. It’s an array with 3 elements:

  • an Integer: HTTP status code
  • a Hash: Some headers
  • an Array: The response body
map <span class="hljs-string">'/posts'</span> <span class="hljs-keyword">do</span>
run Proc.<span class="hljs-keyword">new</span> { |env| [<span class="hljs-string">'200'</span>, {<span class="hljs-string">'Content-Type'</span> => <span class="hljs-string">'text/html'</span>}, [<span class="hljs-string">'first_post'</span>, <span class="hljs-string">'second_post'</span>, <span class="hljs-string">'third_post'</span>]] }
end

Each Rack app is required to return the array with 3 elements. Hence once we have a middleware to modify the response of Rack app, then it’s there.

To read more: Proper Rack Middleware Ordering | Verbose Logging – this is really useful website that help me to understand how the chain of middlewares are interacting with each other.

String interpolation in Ruby

greek_to_roman_gods =
{
zeus: 'Jupiter',
hera: 'Juno',
poseidon: 'Neptune',
hades: 'Pluto',
aphrodite: 'Venus',
ares: 'Mars',
artemis: 'Diana',
athena: 'Minerva',
hermes: 'Mercury',
demeter: 'Ceres'
}

"A heavenly marriage: #{greek_to_roman_gods[:zeus]} and #{greek_to_roman_gods[:hera]}"

format('A heavenly marriage: %s and %s', greek_to_roman_gods[:zeus], greek_to_roman_gods[:hera])
format('A heavenly marriage: %{zeus} and %{hera}', greek_to_roman_gods)

'A heavenly marriage: %s and %s' % [greek_to_roman_gods[:zeus], greek_to_roman_gods[:hera]]
'A heavenly marriage: %{zeus} and %{hera}' % greek_to_roman_gods

# All of them return the same result
=> "A heavenly marriage: Jupiter and Juno"

If you save that file, and run rubocop on that file. You will get the offense Favor format over String#% for line 18-19. Look further into the rubocop gem, it’s coming from  RuboCop::Cop::Style::FormatString

This cop enforces the use of a single string formatting utility. Valid options include Kernel#format, Kernel#sprintf and String#%.

The detection of String#% cannot be implemented in a reliable manner for all cases, so only two scenarios are considered - if the first argument is a string literal and if the second argument is an array literal.

I listed 5 ways to do string interpolation. Let’s go through each of them:

  1. String interpolation in double-quoted string
  2. format with additional arguments
  3. format with a hash argument
  4. String#% with a hash
  5. String#% with an array

The first, and the common one, using the hash symbol in double-quoted string.

"A heavenly marriage: #{greek_to_roman_gods[:zeus]} and #{greek_to_roman_gods[:hera]}"

Then we can go ahead and use sprintf, or there is an alias for that in ruby: format. You can supply a number of variables to replace the %s

format('A heavenly marriage: %s and %s', greek_to_roman_gods[:zeus], greek_to_roman_gods[:hera])

Or you can supply a Hash, and format replace the value of matching hash key:

format('A heavenly marriage: %{zeus} and %{hera}', greek_to_roman_gods)

The last two lines use String#%. Here is the doc:

str % arg → new_str
Format—Uses str as a format specification, and returns the result of applying it to arg. If the format specification contains more than one substitution, then arg must be an Array or Hash containing the values to be substituted. See Kernel::sprintf for details of the format string.

rubocop doesn’t approve using String#% for some stated reason above, then we can change safely to use format instead.

TIL

non-breaking space

non-breaking space is a space that does not allow a line break to be inserted in that space.

In Sublime, you can find whether your document has a non-breaking space by hitting Ctrl + F, then type in Alt + Space. This is a non-breaking space. And Sublime will highlight if we have that in our document.