Idiosyncratic Ruby

Browse by Category

75 | Ruby TRICKS of 2018

Ruby was initially designed to be a successor of the Perl programming language, which also means that it inherited a lot of Perl's expressiveness. To celebrate this, the TRIC¹ contest was invented:

  • Write the most Transcendental, Imbroglio Ruby...
READ

74 | Super Snakes

Have you ever been confused by the __underscores__ required by some of __RUBY__'s features? You can get it right with this overview of all of "super snake" keywords and methods.

READ

73 | Unicode Version Mapping

The Ruby core team cares a lot about Unicode, and this is why we have pretty good Unicode support in the language.

Even though the Unicode standard evolves constantly - it gets updated at least once a year - Ruby's Unicode support is often only a little bit behind the current version of Unicode. The following tables list which Ruby version supports which version of Unicode / Emoji:

READ

70 | Nothing to Escape

What is your wild guess: How many different ways does Ruby provide for inserting a NULL byte into a double-quoted string?

READ

68 | Assignments In-Style

The introduction of pattern matching in Ruby 2.7 brought us a new style of multi-assigning local variables: The pattern assignment, or, how you could also call it, the assignment in-style.

READ

64 | What the Regex?

Regexes, the go-to-mechanism for string matching, must not only be written, but also need to be applied. This episode acts as a reference with some style advice for working with regular expressions in Ruby. If you are looking for resources on writing the actual regexes, take a look at the link collection at the bottom.

READ

63 | idiosyncratic_eval

When you get farther upwards the steep hill that is Ruby mastery, you will come across some powerful, yet slightly evil methods: instance_eval and class_eval¹. They allow you to execute code and define methods tied to a specific class, at the same time giving you access to outer scope variables through the Ruby block syntax. Their exact behavior varies, depending on the context they are used in. So what is the difference between all the evals?

¹ Also aliased as module_eval

READ

61 | Meta Escape Control

Double-quoted strings can not only be used with interpolation, #{}, they also support various escape sequences, which are initiated with \. Escape sequences allow you to embed raw byte and codepoint values. Furthermore, there are shortcuts for common formatting and control characters.

READ

58 | Magic Instructions

Ruby supports magic comments (interpreter instructions) at the top of the source file, mostly known for setting a source files' Encoding. This is the most common use case, but there is more you can do.

READ

57 | What the Time?

%a %A %b %B %c %C %d %D %e %F %g %G %h %H %I %j %k %l %L %m %M %n %N %p %P %Q %r %R %s %S %t %T %u %U %v %V %w %W %x %X %y %Y %z %Z %+ %% - _ 0 ^ # :

Date and time formatting is traditionally done with strftime. Not any different in Ruby, which includes a public domain based strftime implementation accessible via Time#strftime. Ruby would not be Ruby if it would not add some confusion: There is a second implementation included in the standard library which is used by Date#strftime and DateTime#strftime. It behaves similarly in most cases, but also differs in some nuances (for example, additional formatting directives like %Q are supported).

READ

54 | Try Converting

Similar to metaprogramming, Ruby's type conversion system has evolved over time. While the result functions, it is also a little inconsistent and suffers from poor naming. Let's put things in perspective:

READ

53 | The Constant Tree

Generated with Ruby 3.2.2, including RubyGems and DidYouMean:

Object (Class)
├─ARGF (ARGF.class)
├─ARGV (Array)
├─ArgumentError (Class)
├─Array (Class)
├─BasicObject (Class)
│ └─BasicObject → BasicObject
├─Binding (Class)
├─CROSS_COMPILING (NilClass...
READ

52 | Constant Visibility

It is less common, but similar to methods, constants have a visibility attached to them. You have the choice between private and public, and you can also mark a constant deprecated!

READ

50 | Naming Too Good

Some words should not be chosen as identifiers for variables, parameters and methods in Ruby, because they clash with core methods or keywords.

READ

48 | Ruby TRICKS of 2015

Ruby was initially designed to be a successor of the Perl programming language, which also means that it inherited a lot of Perl's expressiveness. To celebrate this, the TRIC¹ contest was invented:

  • Write the most Transcendental, Imbroglio Ruby...
READ

47 | Ruby TRICKS of 2013

Ruby was initially designed to be a successor of the Perl programming language, which also means that it inherited a lot of Perl's expressiveness. To celebrate this, the TRIC¹ contest was invented:

  • Write the most Transcendental, Imbroglio Ruby...
READ

46 | The Art of Arguments

There is nothing easier than parsing the command-line arguments given to your Ruby program: It is an array found in the special variable $*:

READ

43 | New Ruby Startup

What happens when you invoke the Ruby interpreter, even before it executes your first line of code? Actually a lot! A few observations:

READ

42 | R(u)b(y)Config(uration)

At some point when working with Ruby, you come across this mysterious RbConfig constant. A typical scenario is that you want to check which operating system your current program is executed on. You can do this with RbConfig::CONFIG['host_os'] or RbConfig::CONFIG['arch'], see the RubyGems source for an advanced example!

READ

37 | Static Monkeys vs. Strong Ducks

Programming languages have been, and will always be categorized by their typing system. Naturally, large parts of the Ruby community (including myself) have some kind of aversion against static typing. But while Ruby goes down the route of being dynamically typed that does not mean that you are not allowed to use some form of types!

2020 update: Ruby 3.0 introduced types

READ

36 | ERB Render Standard

ERB stands for <%# Embedded Ruby %> and is the templating engine included in the Ruby Standard Library. While there are more recent gems that provide a better templating experience (see tilt for an abstraction, and erubis/erbse for an updated ERB), it is also convenient to have basic support directly in the standard library.

READ

34 | Utility Gems

RubyGems is bundled with core Ruby since 1.9, which was first released in 2007. As long as you do not run Ruby with the $ ruby --disable-gems flag, it is available to you without having to install anything. This also means that you can use some of RubyGems' support utilities for free!

READ

33 | Too Expressive & * _ ?

Ruby's syntax is so expressive — it utilizes every printable, non-alphanumeric ASCII character as much as it cans. Sometimes, this can be confusing for beginners.

READ

32 | No More Errors

If you don't like errors in your code, you will have to fix them. This handy list of Ruby's errors will hopefully help you do so! (And welcome back for the second season of Idiosyncratic Ruby!)

READ

31 | Roots of Rubyism

This is a summary of reasons why you should still use Ruby:

It is a language with a terrific community (welcoming and always questioning itself) and ecosystem (a lot of problems already solved), which is beautiful (focus on productivity), a little conservative (in the sense that it is easy to work with), and general purpose (a good choice in most cases).

For a deeper understanding of Ruby's principles, reading matz is highly recommended:

READ

29 | Limitations of Language

If you take a closer look, you'll notice that Ruby's grammar has quite a few edge-case where its syntax is inconsistent or ambiguous:

READ

27 | Golfing Basics

Code Golf is the art of writing the shortest program possible. The less bytes the better. And the competition is just ridiculously strong! Head over to Anarchy Golf if you want to see more!

READ

24 | Goto Fail

If you change one line in Ruby's source, it will support goto statements!

READ

20 | Better Standards

A guide to Ruby's stdlib and available alternatives.

Please note: The standard library was turned into gems, see stdgems.org for up-to-date information on all standard gems!

Standard Library Overview

READ

17 | Stream Editing

One of Ruby's goals was to replace popular unix stream editors like awk or sed, which both have the concept of manipulating files in a line-based manner. Ruby has the -n option for this:

READ

16 | Changing the Rules

Ruby allows you to change key functionality of the language. This means, it is also possible to break key functionality! Six examples of Ruby code you should never use:

READ

14 | Meeting some Locals

There are two very different ways to create local variables in Ruby. You are probably familiar with the classical way:

READ

13 | Slicing Rubies

Ruby puts a lot of effort into its Enumerable module, offering a lot of different ways of iterating through collections. It is one of the reasons for Ruby's success, but you can also call it idiosyncratic, sometimes. This episode portraits enumerables' three handy slice_* methods.

READ

12 | More Inspections

Some of IRB's command-line options can be called idiosyncratic as well. Take math mode¹ as an example: It will require the infamous mathn library on start up:

READ

9 | Globalization

This is an overview of all the special, two-letter (and other) global variables in Ruby, which Ruby inherited from Perl.

For the purpose of improving code readability, Ruby comes with English.rb in its standard library (or Deutsch.rb as gem), which provides non-cryptic aliases and some documentation.

Ruby also defines some three-letter global variables that mirror CLI options ($-…)

READ

6 | Run Ruby, Run!

There is a one-liner on the internet that will start a local web server, for serving all the static files in your current directory:

READ

5 | Constant Shadows

The script lines feature is probably the most famous example for idiosyncratic naming in Ruby!

READ

3 | Ruby, Can You Speak Louder?

Ruby has some ways to turn on debug mode, which library authors can use to print out extra information for interested users. But unfortunately, there are multiple debug modes in Ruby. When to use which one?

READ

2 | Ruby String Magic

Ruby strings are powerful. You could say Ruby is built around manipulating strings. There are tons of ways how to work with strings and as of Ruby 2.7.1 String offers 130 instance methods. Knowing them well can save you a lot of time.

READ

1 | Test Highlights

Compared to other languages, Ruby does not have very good tool support for development. This might not be a problem for many of us: In the end, humans create the code, and not the tools.

Nevertheless, it would be great to have better tools. Or at least valid syntax highlighting!

READ

Fork me on GitHub