Topic: Get the number in a string

I have a cookie where I store an id and a company name like

2 CompanyName

How do I extract the number from that string? Been trying to figure it out by looking at Ruby Core and String Class but I don't understand what to use.

Re: Get the number in a string

Is it really the id, followed by a space, followed by a company name with nos paces?  If so:
number = coookie[:my_coookie].value.split(' ').first

.value is what gets the string from the cookie (cookies are kinda weird in Rails)
.split breaks the string into an array - cutting it on each ' ' character
.first takes the first part of that array.  Which is the number.

Re: Get the number in a string

danger wrote:

Is it really the id, followed by a space, followed by a company name with nos paces?  If so:
number = coookie[:my_coookie].value.split(' ').first

.value is what gets the string from the cookie (cookies are kinda weird in Rails)
.split breaks the string into an array - cutting it on each ' ' character
.first takes the first part of that array.  Which is the number.

Excellent!

Re: Get the number in a string

Do you have control over storing the cookie value? Out of curiosity, why are you storing the company name in the cookie along with the id? Usually the name is in the database so it can be fetched if you have the id. If you are caching the name so you can fetch it without a call to the database, there are often better ways to do this than through a cookie (which can easily go out of sync if you change the company name for example).

Railscasts - Free Ruby on Rails Screencasts

Re: Get the number in a string

Do you have control over storing the cookie value? Out of curiosity, why are you storing the company name in the cookie along with the id?

that's what i was wondering.  Normally, you could just leave the ID in the cookie and perform a lookup for the rest of the info.  Or, if you are worried about performance, you could serialize the Company object directly into the cookie (i dont use this method much, but it can be done).


peace


-jake

---------------------------------------------------------------------
Rails Development - Agile rails consulting and development for startups on a budget
Flvorful Blog - Some ramblings
Flvorful's Open Source Projects - Some handy hacks for your rails projects.

Re: Get the number in a string

I store the company in the db. The company, the user, adds groups. People taking the survey are users that are anonymous logging in with a group username and password. In order to keep track of who has answered I use a cookie, the only way I know possible. If they erase the cookie that's to bad but not a big deal. Everything else, connected to the company behind the anonymous user, I store in the session var.

The id and company is used to get an unique identity of the cookie. If the person logs in and the cookie is there but the row in the db somehow is missing I add it again with the value in the cookie.

Last edited by palb (2006-12-29 14:23:21)