-2

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?

Stefan
  • 109,145
  • 14
  • 143
  • 218
Darko
  • 535
  • 1
  • 6
  • 15
  • 5
    what you want to achieve not clear,give some examples. show your code. – Arup Rakshit May 13 '13 at 17:22
  • 1
    Regular expressions are made specifically for this sort of thing. Why are you opposed to using them? – tadman May 13 '13 at 17:30
  • That would change a big portion of my code. I was hoping there is some similar funct that just does things from the start of the string. I guess I'll have to go with regex after all – Darko May 13 '13 at 17:42
  • 1
    Why would it change a big portion of your code? It sounds like your code isn't factored correctly. If you'd show examples of your code we could help fix that. – the Tin Man May 13 '13 at 17:44
  • All the answers are given with the blind guesses, and you are responsible for this. Never post such in-completed questions. – Arup Rakshit May 13 '13 at 17:44

2 Answers2

3
[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"
Mori
  • 27,279
  • 10
  • 68
  • 73
0

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.

Sébastien Le Callonnec
  • 26,254
  • 8
  • 67
  • 80