开发者

ROR + Ruby Function To Access Last Value Of An Variable

开发者 https://www.devze.com 2023-04-05 13:38 出处:网络
In Ruby 1.8.6, I want to the last value of any string. For example : notice_5 here, I want \"5\" in one of the variable. So please suggest any function through which 开发者_StackOverflow社区I can ge

In Ruby 1.8.6, I want to the last value of any string.

For example : notice_5 here, I want "5" in one of the variable. So please suggest any function through which 开发者_StackOverflow社区I can get 5 in one of the variable.

Thanks


Use index -1

var = "somevar_5"
var[0]
 => "s"
var[-1]       # get character code
 => 53
var[-1].chr   #get character
 => "5"


Got the Solution

>> a = 'hello_7'
=> "hello_7"
>> a.last
=> "7"
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号