Working with Ruby
Occasional blog posts about Ruby updates, tools, editor tweaks, and random snippets. You might also be interested in my newer project that docuements lesser-known features in Ruby: Idiosyncratic Ruby.

Idiosyncratic Ruby

May 2015, one-liners · regex · golf · fun · docs · irb · thoughts · tutorial · hints · stdlib · ruby

For the whole of May, I will blog about weird things in Ruby over at Idiosyncratic Ruby – Don’t miss it!

Be More Productive with Better Sublime Snippets for Ruby!

Long time readers of this blog will remember that I used to tweak GNOME’s gedit editor a lot. However, I ditched it for Sublime Text and I am not looking back. Here are some of the things I like about Sublime:

  • Offers a good out-of-the-box experience, including an amazing “fuzzy search” tool (ctrl+p)
  • It works on ubuntu without any problems
  • Fast (enough)
  • Multiple cursors!
  • Once you have installed Package Control: An integrated extension repository
  • Encourages you to write your own extensions
  • Looks good

Ruby and Random

Wrongly used randomness can be the source of hard-to-detect bugs and security holes. This is relevant every time you use randomness, for example, when implementing an existing protocol/interface that requires random values or generating tokens for your next raffle. This article describes when to use which of Ruby’s randomness methods.

…read

ActiveSupport 4 by Example: Numeric#to_s

The NumberHelper functionality is now available as Numeric#to_s

…read

value_struct: Read-only structs in Ruby

Ruby’s structs are one of my favorite data types in Ruby. They help you to keep some defined structure in the dynamic world of Ruby. Often, it makes sense to use them instead of hashes or arrays. Read-only structs take the idea a level further.

…read

6 basic cd tricks you should know and use!

And you’ll never want to miss them again!

…read

RubyBuntu -6- gedit 3! wtf? set it up for ruby/web development :)

So you’ve installed (or upgraded to) ubuntu 11.10 and everything looks great… Except – uh!, lots of gedit plugins are only compatible with gedit 2! But don’t be sad.. or angry.. This guide points out, how to, nevertheless, create a solid foundation that allows you getting prodcutive with gedit!

…read

Fixing a little anchor permalinks vs. pagination issue (using javascript and jekyll)

I am using the Jekyll blog generator for some projects (still not sure if I really like it..) and came across the following requirement the other day:

  • The blog should only have an index page, no (visible) sub-posts
  • This index page should use pagination
  • There should be the ability to link to a specific blog post

The problem was: I wanted to use anchor links (http://some.url/#post-identifier), but – because of the pagination – blog posts “change” their index page (e.g. in jekyll, they move from /#example to /page2/#example).

…read

Pass multiple blocks to methods using named procs

My codebrawl entry (gist) got the 5th place (out of 20). Want to use it in real life :)

…read

Three little tips for slimmer Rails migrations

Rails migrations are easy to understand and easy to write. However, you can save some unnecessary key strokes by applying these three tips :)

…read

eurucamp: Tweak your Ruby consoles - 2011 edition

Exploring the stdlib: logger

puts is great for a quick output, but when a script gets more complex or you want to offer a flexible executable, consider using the logger ;). Since the logger class is part of the Ruby standard library, it can be used everywhere, without installing any gems. And it’s very easy to use :D

…read

♥ .railsrc / rails console snippets

13 Rails-specific hints for your rails 3 console.

…read

Small Ruby CLI Improvements (Part 4): Edit Ruby Objects Using yaml

Somehow, I stumbled upon this useful little script by _why: Update

…read

Small Ruby CLI Improvements (Part 3): Hirb vs. Unicode

Hirb: “A mini view framework for console/irb that’s easy to use, even while under its influence. Console goodies include a no-wrap table, auto-pager, tree and menu.” now supports unicode in two ways:

┌───────────────────────────────────────────┬───────────────────────────────────────────────┐ │ Support for unicode full-width characters │ Unicode table characters instead of +/- chars │ └───────────────────────────────────────────┴───────────────────────────────────────────────┘

…read

Small Ruby CLI Improvements (Part 1): Command-line Regex Debugging

This little method (now also available in zucker/debug) is useful for understanding and creating regexes:

…read

Requirements: A Mini RubyGems plugin

The .gemspec file of a gem allows to specify requirements for that gem – but usually you do not get to see them. These five lines patch RubyGems, so that gem displays the requirements of a gem after it has been installed:

…read

Project Euler 19, 20, 21, 22, 23, 24, 25 (Ruby)

The next pack of Project Euler solutions.

…read

Little heplers for Ruby print debugging

Not everyone likes debuggers. I rather print the debug values myself – it works and I do not need to learn a debugger :P

…read

How to properly check for your Ruby interpreter, version and OS

Zucker 4 adds accessors to some environment information:

  • OS: returns the current operating system
  • RubyEngine: returns the current Ruby implementation
  • RubyVersion: returns the current Ruby version

And here is how it works.

…read

Introducing Ruby Zucker - a new syntactical sugar gem

Zucker is a collection of lightweight scripts (cubes) that make Ruby even more beautiful: rubyzucker.info

…read

The multi mega method list

One of my favourite ways of learning something about existing code is to load it into irb and play around with it. You are able to ask every object in irb what it can do. It is as easy as you just asking for methods or public_methods and the object will show its abilities. But often you get spammed by Object or irb methods that you rarely want to use.

…read

Project Euler 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 (Ruby)

More solutions for the projecteuler.net problems. Some of them are solved using 1.9.2 features.

…read

Become a Proc Star!

One useful (and funny) feature of Ruby is the Symbol#to_proc method that lets you write concise code like this: %w|1 2 3 4|.map(&:to_i). Almost everyone who knows this feature loves it ;). However, the use cases are pretty limited, because in most cases you need to pass parameters!

…read

New Array and Enumerable methods in Ruby 1.9.2: keep_if, chunk...

In Ruby, dealing with Arrays and similar objects is pretty fun. And we have gotten more possibilities with Ruby 1.9.2 :)

…read

Upgrading to Rails 3: Obstacles and helper scripts

Transferring an existing project to Rails 3 is not that hard, but it usually cannot be done in 5 minutes.

…read

gedit external tools: Ruby helpers, git integration and more

With the easy-to-use (and pre-installed) command line utility zenity, it is pretty simple to create useful external tools for gedit!

…read

RubyBuntu -4- Make gedit better than any IDE ;)

You can, of course, use an IDE for coding – but reconsider it: After hours of initialization, it tries to do everything for you… Somewhere in the never ending menu jungle… And you need to click all the time… Stop! Focus on the gist! You want to write code!

…read

RubyBuntu -3- Be one with your command line!

Most Ruby programmers know: Many things can be done in much less time on the command line. To become more productive, you should take the 10 minutes to configure some basic settings.

…read

RubyBuntu -2- Troubleshooting common Ruby ubuntu problems

A common Ruby experience on ubuntu: You get some stupid error, saying something would be missing – and you don’t know what to do…

…read

NoTabs

After editing some source files with different editors on different platforms, I had some troubles with automatically inserted tabs messing it all up.

This little script replaces all tabs with two spaces.

…read

Organise your code comments with rake notes:todo!

Lots of IDEs (e.g. Netbeans) and some editors (e.g. gedit with plugins) have a nice feature: They show comments, which start with something like TODO or FIXME. Those annotations are quickly written and they make it harder to forget some things you wanted to (or have to) do.

I have just discovered that Rails has this feature already built-in!

…read

Oh, this sweet and tasty syntactic sugar!

This article is written for people with experience in programming in general, but who are new to Ruby.
A German version is published in the offline magazine #2, a magazine by some students of TU Dresden.

The intention is to demonstrate some features of Ruby and show, what is so great about Ruby:

A clean syntax combined with the possibility to adapt the language to given requirements flexibly.

…read

Quicksort in 5 minutes

Some time ago, I conducted a short presentation about Ruby. And to impress the audience, I did some live coding and implemented the quicksort algorithm in 5 minutes. They were impressed :)

…read

Troubleshooting an aegis-permission problem

In my current Rails project, I use the aegis gem for rights management. And I almost got mad, wondering, why it wouldn’t work..

…read

Create an offline version of the Ruby docs

When I began programming Ruby/Rails, I quickly found the online Ruby documentation at ruby-doc.org and the Rails API, which are both very useful. But unfortunately, one cannot be always online. In this blog post, I’ll demonstrate some ways to generate or get the docs offline and some hints on using them.

…read

Storing an array of indices in an integer

Sometimes you have an array of indices. These might, for example, act as flags, whether some specific options are set or not. A nice way to store this list is, to store it in one single number.

…read