I have a huge string prepared using an operator <<in a loop. In the end, I want to delete the last 2 characters.
<<
some_loop str << something end str = str[0..-3]
I think the last operation above will consume memory and time, but I'm not sure. I just wanted to see if there is an operation with the opposite effect <<, so I can remove these last 2 characters from one line.
In fact, line cutting is already a fast and memory efficient operation, since the contents of the line are not copied until it is really needed.
. double: Ruby ".
, ; java, C.
, :
str = str[0..-3]
, , , . .
, , ? - ?
names = ['Mary', 'John', 'Dave'] res = '' names.each do |n| res << n << ', ' end res # => 'Mary, John, Dave, '
, .
names.join(', ') # => 'Mary, John, Dave'
: linefeed/newline (CR/LF), String.chomp ( String#chomp!, ).
String.chomp
String#chomp!
:
2.times{ string.chop! }
string.chop! string.chop!