Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the formal term for the "#{}" token in Ruby syntax?

The Background

I recently posted an answer where I variously referred to #{} as a literal, an operator, and (in one draft) a "literal constructor." The squishiness of this definition didn't really affect the quality of the answer, since the question was more about what it does and how to find language references for it, but I'm unhappy with being unable to point to a canonical definition of exactly what to call this element of Ruby syntax.

The Ruby manual mentions this syntax element in the section on expression substitution, but doesn't really define the term for the syntax itself. Almost every reference to this language element says it's used for string interpolation, but doesn't define what it is.

Wikipedia Definitions

Here are some Wikipedia definitions that imply this construct is (strictly speaking) neither a literal nor an operator.

  1. Literal (computer programming)
  2. Operator (programming)

The Questions

Does anyone know what the proper term is for this language element? If so, can you please point me to a formal definition?

like image 545
Todd A. Jacobs Avatar asked Jun 03 '12 11:06

Todd A. Jacobs


3 Answers

Ruby's parser calls #{} the "embexpr" operator. That's EMBedded EXPRession, naturally.

like image 92
Phlip Avatar answered Sep 30 '22 02:09

Phlip


I would definitely call it neither a literal (that's more for, e.g. string literals or number literals themselves, but not parts thereof) nor an operator; those are solely for e.g. binary or unary (infix) operators.

I would either just refer to it without a noun (i.e. for string interpolation), or perhaps call those characters the string interpolation sequence or escape.

like image 38
Asherah Avatar answered Sep 30 '22 00:09

Asherah


TL;DR

Originally, I'd hypothesized:

Embedded expression seems the most likely definition for this token, based on hints in the source code.

This turned out to be true, and has been officially validated by the Ruby 2.x documentation. Based on the updates to the Ripper documentation since this answer was originally written, it seems the parser token is formally defined as string_embexpr and the symbol itself is called an "embedded expression." See the Update for Ruby 2.x section at the bottom of this answer for detailed corroboration.

The remainder of the answer is still relevant, especially for older Rubies such as Ruby 1.9.3, and the methodology used to develop the original answer remains interesting. I am therefore updating the answer, but leaving the bulk of the original post as-is for historical purposes, even though the current answer could now be shorter.

Pre-2.x Answer Based on Ruby 1.9.3 Source Code

Related Answer

This answer calls attention to the Ruby source, which makes numerous references to embexpr throughout the code base. @Phlip suggests that this variable is an abbreviation for "EMBedded EXPRession." This seems like a reasonable interpretation, but neither the ruby-1.9.3-p194 source nor Google (as of this writing) explicitly references the term embedded expression in association with embexpr in any context, Ruby-related or not.

Additional Research

A scan of the Ruby 1.9.3-p194 source code with:

ack-grep -cil --type-add=YACC=.y embexpr .rvm/src/ruby-1.9.3-p194 |
    sort -rnk2 -t: |
    sed 's!^.*/!!'

reveals 9 files and 33 lines with the term embexpr:

test_scanner_events.rb:12
test_parser_events.rb:7
eventids2.c:5
eventids1.c:3
eventids2table.c:2
parse.y:1
parse.c:1
ripper.y:1
ripper.c:1

Of particular interest is the inclusion of string_embexpr on line 4,176 of the parse.y and ripper.y bison files. Likewise, TestRipper::ParserEvents#test_string_embexpr contains two references to parsing #{} on lines 899 and 902 of test_parser_events.rb.

The scanner, exercised in test_scanner_events.rb, is also noteworthy. This file defines tests in #test_embexpr_beg and #test_embexpr_end that scan for the token #{expr} inside various string expressions. The tests reference both embexpr and expr, raising the likelihood that "embedded expression" is indeed a sensible name for the thing.

Update for Ruby 2.x

Since this post was originally written, the documentation for the standard library's Ripper class has been updated to formally identify the token. The usage section provides "Hello, #{world}!" as an example, and says in part:

Within our :string_literal you’ll notice two @tstring_content, this is the literal part for Hello, and !. Between the two @tstring_content statements is a :string_embexpr, where embexpr is an embedded expression.

like image 27
Todd A. Jacobs Avatar answered Sep 30 '22 02:09

Todd A. Jacobs