简体   繁体   English

Ruby感叹号 - 打印下一行

[英]Ruby exclamation mark -printing next line

TENANT_NAME=gets.chomp('/\p{Alnum}/')

$first = TENANT_NAME.slice(0,1).capitalize

$second = TENANT_NAME.slice(1..-1)

EXPORT_PASSWORD="Export-"+ $first + $second + "!"

puts EXPORT_PASSWORD

I want to print EXPORT_PASSWORD in one-line, but when i am trying to print it prints ! 我想在单行中打印EXPORT_PASSWORD,但是当我尝试打印它时会打印出来! from another line. 从另一条线。 I am using Ruby output: 我正在使用Ruby输出:

devi123kumari
Export-Devi123kumari
!
>TENANT_NAME=gets.chomp('/\p{Alnum}/')
somename
TENANT_NAME
=> "somename\n"

see you are getting \\n here which is shifting your ! 看到你正在\\这里正在改变你的! to second line. 到第二行。

>TENANT_NAME=gets.chomp
somename
TENANT_NAME
=> "somename"

now you wont get the extra line. 现在你不会得到额外的线。

Try: 尝试:

EXPORT_PASSWORD="Export-"+ $first + $second.chomp + "!"'

This is because if you check the value of $second , it contains trailing \\n . 这是因为如果你检查$second的值,它包含尾随\\n

Also in Ruby prepending $ to variable names is not a standard practice. 同样在Ruby中,将$加到变量名称不是标准做法。

chomp removes the given argument from the end of the string or carriage return characters (ie \\n , \\r , and \\r\\n ) if no argument is given: 如果没有给出参数, chomp会从字符串或回车字符的末尾删除给定的参数(即\\n\\r\\r\\n ):

"hello".chomp                #=> "hello"
"hello\n".chomp              #=> "hello"
"hello".chomp("llo")         #=> "he"

Your code removes '/\\p{Alnum}/' (that's a string, not a regexp) from the end of a string: 您的代码从字符串'/\\p{Alnum}/'删除'/\\p{Alnum}/' (这是一个字符串,而不是正则表达式):

'hello/\p{Alnum}/'.chomp('/\p{Alnum}/') #=> "hello"

This is probably not what you want. 这可能不是你想要的。 Just use chomp without an argument. 只需使用没有参数的chomp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM