Idiosyncratic Ruby

Browse by Category

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 2.4.0, including RubyGems and DidYouMean:

Object (Class)
├─ARGF (ARGF.class)
├─ARGV (Array)
├─ArgumentError (Class)
├─Array (Class)
├─BasicObject (Class)
│ └─BasicObject → BasicObject
├─Bignum (Class)
│ └─GMP_VERSION (String)
...
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 misterious 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!

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 ambigious:

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

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 potraits 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 build around manipulating strings. There are tons of ways how to work with strings and as of Ruby 2.2.2, String offers 116 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

Feed
Fork me on GitHub