Project

General

Profile

Actions

Feature #20525

closed

Percent string literal with indentation support or String#dedent

Added by bradgessler (Brad Gessler) 6 months ago. Updated 3 months ago.

Status:
Rejected
Assignee:
-
Target version:
-
[ruby-core:118180]

Description

I have code that looks like this in an application:

ContentSlide(title: "Why Phlex?"){
  Markdown <<~MARKDOWN

  * Because its fun
  * Because its super-de-dooper
  MARKDOWN
},

The "squiggle" HEREDOCs strips the indentation, but the name of the HEREDOC doesn't look that great since "Markdown" appears twice.

What I'd prefer is a string literal that deals with indentation, maybe it looks something like this:

ContentSlide(title: "Why Phlex?"){
  Markdown %~{
    # Why do you like markdown?

    * Because its fun
    * Because its super-de-dooper
  }
},

If Ruby doesn't want to go down the path of adding another type of literal, I'd propose adding a method to String that does the same thing as the ~ HEREDOC so something like this is possible:

ContentSlide(title: "Why Phlex?"){
  Markdown %{
    # Why do you like markdown?

    * Because its fun
    * Because its super-de-dooper
  }.dedent
},
Actions #1

Updated by bradgessler (Brad Gessler) 6 months ago

  • Tracker changed from Bug to Feature
  • Backport deleted (3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN)
Actions #2

Updated by bradgessler (Brad Gessler) 6 months ago

  • Subject changed from Percent string literal with indentation support to Percent string literal with indentation support or String#dedent
Actions #3

Updated by bradgessler (Brad Gessler) 6 months ago

  • Description updated (diff)

Updated by nobu (Nobuyoshi Nakada) 4 months ago ยท Edited

It conflicts with an existing syntax.
% plus a punctuation starts a string literal that ends with the punctuation.

Updated by shyouhei (Shyouhei Urabe) 4 months ago

Why not <<~"}" ?

Updated by bradgessler (Brad Gessler) 4 months ago

Actually this looks decent:

Markdown ~{
  # Hi!

  This is markdown
}

Updated by mame (Yusuke Endoh) 4 months ago

  • Status changed from Open to Rejected

This was briefly discussed at the dev meeting, but ended with "why not use a shorter delimiter like <<~END or <<~MD?"

Updated by bradgessler (Brad Gessler) 3 months ago

Same reason you can conjure up a Proc via -> {} โ€” the syntax looks cleaner and you don't have to stop and try to name it.

Not having to name a string is a pretty big boost in terms staying in the flow.

The second thing: it looks ugly having two of the same names by each other, which the original example shows:

markdown <<~MD
  # Hello
  
  How are you doing?
MD

still looks kind of weird.

This looks better:

markdown <<~
  # Hello
  
  How are you doing?
>>

Updated by ufuk (Ufuk Kayserilioglu) 3 months ago

@bradgessler (Brad Gessler) I am not sure if you've missed the suggestion by @shyouhei (Shyouhei Urabe) in https://bugs.ruby-lang.org/issues/20525#note-5:

markdown <<~"}"
  # Hello

  How are you doing?
}

This doesn't need naming anything and has no repetition either.

Updated by bradgessler (Brad Gessler) 3 months ago

Oh interesting, I thought that was a typo ๐Ÿ˜‚.

I was playing around with characters after I wrote this and found something that I think looks a little better than the "}" thing:

markdown <<~___
  # Hello

  How are you doing?
___

I now understand that both of those work, but it's still not as beautiful syntactically speaking as a HEREDOCS that doesn't require a name or work-around.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0