In Ruby, what are the vertical lines?

后端 未结 4 2086
我寻月下人不归
我寻月下人不归 2020-12-20 23:23
1.upto(9) { |x| print x }

Why won\'t this work?

{ print x |x} }

What about y?

4条回答
  •  天命终不由人
    2020-12-21 00:20

    It's not an operator; it's delimiting the argument list for the block. The bars are equivalent to the parens in def foo(x). You can't write it as {print x |x} for the same reason this:

    def foo(x)
      puts "It's #{x}"
    end
    

    can't be rewritten as this:

    def foo
      puts "It's #{x}" (x
    end
    

提交回复
热议问题