Project

General

Profile

Actions

Bug #20474

closed

Heredoc common leading whitespace calculation question

Added by kddnewton (Kevin Newton) 12 days ago. Updated 11 days ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:117794]

Description

I think I understand that <<~ will strip common leading whitespace from all lines. However, I am confused by the following example:

eval("<<~H\n     \nh\n     \nH\n")

In this example, the heredoc has 3 lines: " \n", "h\n", and " \n". The common leading whitespace is definitely 0, because of the 2nd line.

However, the string is equal to "\nh\n\n". In fact, no matter how many spaces you put before the h, it will always be equal to "\nh\n\n".

I'm not sure if this is a bug, or even what is happening here. Could someone explain?

Updated by nobu (Nobuyoshi Nakada) 12 days ago

It looks like a bug.

Actions #2

Updated by nobu (Nobuyoshi Nakada) 11 days ago

  • Status changed from Open to Closed

Applied in changeset git|5bb656e4f063e717262efe2dc303ed914eb1dd6c.


[Bug #20474] Keep spaces in leading blank line

Updated by Dan0042 (Daniel DeLorme) 11 days ago

However, the string is equal to "\nh\n\n".

I would have expected it to be

"     \nh\n     \n"

but ignoring all whitespace from blank lines seems like a convenient feature to me.

In fact, no matter how many spaces you put before the h, it will always be equal to "\nh\n\n".

I'm pretty sure that's normal; many/most editors will strip the indentation of blank lines, so we want common leading whitespace to be computed only for lines that have some non-whitespace character.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0