简体   繁体   中英

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 ! from another line. I am using Ruby output:

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 .

Also in Ruby prepending $ to variable names is not a standard practice.

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:

"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:

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

This is probably not what you want. Just use chomp without an argument.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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