I’m trying to implement the following code in Ruby and getting stuck on it.
I’ve looked at documentation but they all deal with a string rather than an object.
PHP code:
if ( substr($new_price, -2) != '00' ) {
Ruby code I tried:
if new_price[-2,2] != '00'
if new_price.to_s.slice[-2,2] != '00'
Edit:
For current usage, which is just to check to see if a price has decimals or not, I switched to this:
if new_price.ceil != new_price
I am still curious how to do this properly in Ruby.
In Ruby you can get the last two elements of a string with (for example) this code:
You could also add the
last(n)method to the String class, like so:Then you could write code like this: