Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check the last letter of a string?

I have the following code:

<%= "#{@resource.first_name.capitalize} " + "#{@resource.last_name.capitalize}" + "'s Area" %>

which prints stuff like:

Tom Byrne's Area, or Mark Kelly's Area

It would be really cool though if I could avoid things like:

Jean Collins's Area, or Paul Harris's Area

and use the correct grammar like:

Jean Collins' Area, or Paul Harris' Area

Anybody know how I could write some code that would write "'s Area" if the last letter of last_name is not "s", and write "' Area" if it is "s"?

like image 807
CHarris Avatar asked Jan 21 '26 00:01

CHarris


1 Answers

To get the last character of a string: str[-1].

You might look if someone else has done this, since there are several special cases. A quick google search yielded the possessive gem.

like image 174
Eric Andres Avatar answered Jan 22 '26 13:01

Eric Andres



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!