Forum: Ruby on Rails How do I match strings?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7759594c66801d6f311ff38fd3ddef2a?d=identicon&s=25 Chris Gunnels (ballhogjoni)
on 2009-03-17 23:24
Ok I know that sounds newbish, but I don't want to match the entire
string. I just want to match the first 25 characters or so.

I have a hash that looks like

somestuff[:something]

Now I want somestuff[:something] to equal a string.

somestuff[:something] == 'sadfasdfsdfasdf'

the problem is that somestuff[:something] will never match the entire
string, so I want to match the first 25 characters.

Any ideas...I hope that made sense?
2505b282d57c29be797dc35b245adb4c?d=identicon&s=25 Philip Hallstrom (Guest)
on 2009-03-17 23:42
(Received via mailing list)
On Mar 17, 2009, at 3:24 PM, Chris Gunnels wrote:

> somestuff[:something] == 'sadfasdfsdfasdf'
>
> the problem is that somestuff[:something] will never match the entire
> string, so I want to match the first 25 characters.
>
> Any ideas...I hope that made sense?

somestuff[:something][0,25] = "asdfasdfasdf"

Is one way.  There's probably a ruby method to match N characters of a
string similar to C's strncmp too.
280b78a61a968391b7e07e912be102a8?d=identicon&s=25 Robert Walker (robert4723)
on 2009-03-18 00:34
Chris Gunnels wrote:
> Ok I know that sounds newbish, but I don't want to match the entire
> string. I just want to match the first 25 characters or so.
>
> I have a hash that looks like
>
> somestuff[:something]
>
> Now I want somestuff[:something] to equal a string.
>
> somestuff[:something] == 'sadfasdfsdfasdf'
>
> the problem is that somestuff[:something] will never match the entire
> string, so I want to match the first 25 characters.
>
> Any ideas...I hope that made sense?

There are a number of ways depending on your needs. In your case if you
just want to match the beginning of the string Rails provides the
starts_with method (Ruby 1.9 provides start_with, but not Ruby 1.8). For
more complex matches Ruby has excellent regex support. Look at the ~=
method.

 somestuff[:something].starts_with "whatever"

somestuff[:something] ~= /^whatever.+/i
7759594c66801d6f311ff38fd3ddef2a?d=identicon&s=25 Chris Gunnels (ballhogjoni)
on 2009-03-18 16:52
Robert Walker wrote:

> just want to match the beginning of the string Rails provides the
> starts_with method (Ruby 1.9 provides start_with, but not Ruby 1.8). For
> more complex matches Ruby has excellent regex support. Look at the ~=
> method.
>
>  somestuff[:something].starts_with "whatever"


Thanks for the info...

Can "whatever" be a variable?
This topic is locked and can not be replied to.