To get the first three characters of a string, we can use Ruby's slice
method with a range of 0..2
, which would extract the characters at positions 0, 1, and 2:
main.rb78 chars4 lines
To get the last five characters of a string, we can use negative indexing to count from the end of the string. We can use slice
again, this time with a range of -5..-1
:
main.rb55 chars3 lines
Finally, we can concatenate the two substrings using the +
operator:
main.rb60 chars3 lines
We can also use string interpolation to accomplish the same thing:
main.rb65 chars3 lines
gistlibby LogSnag