문제

I am looking for an alternative that would solve the following issue:

with .detect if I am looking for stringvalue and the value given is 132stringvalue, it will still return true. I need an alternative that will be matching everything from the beginning of the string. Any suggestions aside from regex?

도움이 되었습니까?

해결책

[1] pry(main)> %w[foo 456foo bar 123bar].detect {|e| e.to_i > 0}
=> "456foo"
[2] pry(main)> %w[foo 456foo bar 123bar].detect {|e| e.start_with?('123')}                   
=> "123bar"

다른 팁

You can use String#[] (see doc):

1.9.3p194 :001 > "123stringvalue"["stringvalue"]
 => "stringvalue" 
1.9.3p194 :002 > "123stringvalue"["blah"]
 => nil 

It returns the substring if present, nil otherwise.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top