2
{ |n| "user#{n}" }

The curly braces with two pipes. This is a ridiculous question, I know, but trying to google what the name of this syntax is has proven to be impossible.

mu is too short
  • 426,620
  • 70
  • 833
  • 800
  • There are those who call the two pipes, `|x|`, "goal posts"... – Phlip May 02 '18 at 00:23
  • You'll often see them being called "closures". The language itself seems to call them blocks, there's the `block_given?` method for example. – Kimmo Lehto May 02 '18 at 06:30

2 Answers2

5

It’s called a block. There are two types of blocks in ruby. One is this syntax in curly braces and second is the do end. These both are interchangeable but the curly braces have higher precedence. Therefore these programs are interchangeable:

my_array.each do |element|
  puts element
end


my_array.each {|element|
  puts element
}

This post discusses in good detail the difference between the two and when they aren’t interchangeable.

UsamaMan
  • 695
  • 10
  • 28
1

This appears to be called a "code block".

It seems to essentially be an anonymous function. n is the parameter of the function in this case.

Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
  • cool, that link is exactly what I needed! I'll accept your answer in 6 minutes or whatever. :) –  May 02 '18 at 00:17
  • blue, why the rush to select an answer? Quick selections may discourage other answers and short-circuit others working on answers. There's no rush. – Cary Swoveland May 02 '18 at 00:23
  • I mean, because the question is answered? –  May 02 '18 at 00:26
  • I don't see a problem with rushing to accept an answer. I believe this shouldn't steer other people off writing better answers. @blue totally can change his mind about what answer to accept after the fact. – D-side May 02 '18 at 00:39