Ruby Blocks

In this article we will discuss how default arguments work and analyze a situation where they can be useful.

The Ruby block

Ruby blocks are code that can be run by other code. All methods can accept blocks, but it’s up to them whether they do something with them or not.

3.times { puts 'hello world' }
3.times do
  puts 'hello world'
end

Blocks can take parameters

Ruby blocks accept parameters and can also initialize them with default values.

def call_block(&block)
  block.call
end

call_block do |greeting = "hi"|
  puts "Block value: '#{greeting}'"
end

In the example above, we initialize the local block variable greeting with the value “hi”.

The behavior can be demonstrated more easily if we use procs:

is_even_proc = Proc.new {|n=1| n%2 == 0 }

is_even_proc(2) # => true
is_even_proc()  # => false, which is the default behavior

Usefulness

For a real example let’s consider the shared examples feature from RSpec, the well know gem for behavior driven development inside the Ruby ecosystem. So, when we use shared_examples we can define a default value and thus avoid code duplication.

# pseudocode

shared_example success |cost=0|
  it "processes the order" do
    expect_any_instance_of(Product).to receive(cost)
  end
end

# Can be used with or without cost

it_behaves_like :success, 1
it_behaves_like :success

Conclusion

To sum up, Ruby allows blocks to receive parameters and enables its initialization with default values. This is applicable is some real world scenarios, such as DRYing up our RSpec suite.

Productivity Tools

In this article we will discuss some every day productivity tools.

Intro

The list contains tools available to all known operating systems, for the purposes of this article I chose a Debian distribution. You can scan the list for any interesting findings or just explore one by one the presented tools.

1. Time management – Pomodoro

The pomodoro technique is a time management technique that uses a timer to break down work into intervals.
My tool of choice is the because it integrates seamlessly with my desktop. It is available for Linux, even though there are similar pomodoro applications for all operating systems.

2. Note management – Note Board

Note board is a productivity app for taking notes(!!). I use only the chrome extension as it supplement my workflow. In particular, I use it for saving small bits of any webpage for later study or further categorization into the google drive notes (see later).

3. Cloud storage – Google drive

Google drive is a free cloud storage solution by Google. It enables seamless sync with my android phone and my tablet, a good enough text editor and basic version control.

4. Reading list management – Pocket

Pocket is productivity tool which helps you manage and tide up your web reading list(you can also use it for videos). I use the chrome extension. The cool thing about pocket is that you can easily download the android app and have an offline version of your whole reading list in your phone, so you can save on your mobile plan.

5. Text editor – Vim

Vim is a highly configurable and by default extremely lightweight text editor. I use vim to write code, notes, blog posts and really anything. It’s clearly a personal choice here, so use any text editor you feel productive with.

6. Task management – Taskwarrior and Todoist

I am a fan of organised and well thought to-do lists that supplement my workflow and not dictate it. For this reason, I am using two tools to manage my to-do tasks.
For simple things that I also need to have available at my phone, I use Todoist , which provides a chrome extension and a phone application.

For more work related things or more complex tasks that for example have big descriptions I use Taskwarrior . Taskwarrior is Free and Open Source Software that manages your TODO list from the command line.

7. Version control – Git

Finally, I cannot think how my workflow would be without the ability to create different versions of the same document either for testing or experimenting with an idea without keeping many files like file1.txt, file1_1.text etc. Git enables you to do exactly that. Git is for everyone who could benefit from the aforementioned workflow and not only for developers.

With this in mind, I also use Github to host in the cloud any git directories.

TL;DR

Productivity is a complex problem and finding the right tools for your workflow is a worthwhile quest. On the positive side, when I learned good enough the presented tools my workflow became more standardized and I stopped wasting time thinking about where to store a temporary small note(note board, vim) or when to take a break(pomodoro). I hope you find something useful in this list.