Search code examples
rubyruby-1.8.7

Ruby remove everything after first space


<%= @contact.foo_help %>

Outputs a number id and title (with a space between them), ex: 29292 This Is A Title. I just want the number. It won't always be the same amount of digits, and ocassionly I use numbers in my titles.

I was thinking the easiest way would be to gsub everything after the first space out, but I'm two weaks into this framework and can't get the synstax right! Please help

<%= @contact.foo_help.gsub( \s ' ')  %>

Solution

  • @contact.foo_help.gsub(/\s.+/, '')
    

    Will match a space followed by one or more of any characters, and replace with an empty string.

    Rubular is wonderful for this sort of thing http://rubular.com/