I am furiously trying to find what I am looking for in Pickaxe book and not finding it. I''m getting some values back from a posted form and I need to get the ''id'' number off the backend. The ''String'' will always come back to me as... Some_name\r\n123 where 123 is actually the id number which I need to execute the find. The reason they are coming back this way is from a composite ''text_auto_complete_for'' methodology which is allowing users to pick by name and I want the associated id column for an easy find. But I can''t get to square one with irb... irb(main):021:0> myvar = "Some_name\r\n123" => "Some_name\r\n123" irb(main):022:0> show_regexp(myvar, /\w+/) NoMethodError: undefined method `show_regexp'' for main:Object from (irb):22 from :0 Please someone... a. tell me what type of command I would need to pick the numbers off the end b. tell me why irb doesn''t do something so obviously in the Pickaxe book. Thanks Craig
Craig White wrote:> I am furiously trying to find what I am looking for in Pickaxe book and > not finding it. > > I''m getting some values back from a posted form and I need to get the > ''id'' number off the backend. The ''String'' will always come back to me > as... > > Some_name\r\n123So you actually have a multi-line string being returned...> where 123 is actually the id number which I need to execute the find. > The reason they are coming back this way is from a composite > ''text_auto_complete_for'' methodology which is allowing users to pick by > name and I want the associated id column for an easy find. > > But I can''t get to square one with irb... > > irb(main):021:0> myvar = "Some_name\r\n123" > => "Some_name\r\n123" > irb(main):022:0> show_regexp(myvar, /\w+/) > NoMethodError: undefined method `show_regexp'' for main:Object > from (irb):22 > from :0 > > Please someone... > > a. tell me what type of command I would need to pick the numbers off the > end > > b. tell me why irb doesn''t do something so obviously in the Pickaxe > book.
On Wed, 2006-03-08 at 22:53 -0500, Brian V. Hughes wrote:> Craig White wrote: > > I am furiously trying to find what I am looking for in Pickaxe book and > > not finding it. > > > > I''m getting some values back from a posted form and I need to get the > > ''id'' number off the backend. The ''String'' will always come back to me > > as... > > > > Some_name\r\n123 > > So you actually have a multi-line string being returned... > > > where 123 is actually the id number which I need to execute the find. > > The reason they are coming back this way is from a composite > > ''text_auto_complete_for'' methodology which is allowing users to pick by > > name and I want the associated id column for an easy find. > > > > But I can''t get to square one with irb... > > > > irb(main):021:0> myvar = "Some_name\r\n123" > > => "Some_name\r\n123" > > irb(main):022:0> show_regexp(myvar, /\w+/) > > NoMethodError: undefined method `show_regexp'' for main:Object > > from (irb):22 > > from :0 > > > > Please someone... > > > > a. tell me what type of command I would need to pick the numbers off the > > end > > > > b. tell me why irb doesn''t do something so obviously in the Pickaxe > > book.---- apparently, a multi-line string is being returned... Yes Craig
On Mar 8, 2006, at 7:48 PM, Craig White wrote:> I am furiously trying to find what I am looking for in Pickaxe book > and > not finding it. > > I''m getting some values back from a posted form and I need to get the > ''id'' number off the backend. The ''String'' will always come back to me > as... > > Some_name\r\n123 > > where 123 is actually the id number which I need to execute the find. > The reason they are coming back this way is from a composite > ''text_auto_complete_for'' methodology which is allowing users to > pick by > name and I want the associated id column for an easy find. > > But I can''t get to square one with irb... > > irb(main):021:0> myvar = "Some_name\r\n123" > => "Some_name\r\n123" > irb(main):022:0> show_regexp(myvar, /\w+/) > NoMethodError: undefined method `show_regexp'' for main:Object > from (irb):22 > from :0 > > Please someone... > > a. tell me what type of command I would need to pick the numbers > off the > end/(\d+)$/ Answer in $1> b. tell me why irb doesn''t do something so obviously in the Pickaxe > book.Page 93 of the Pick Axe shows give this for show_regex... def show_regexp(a, re) if a =~ re "#{$`}<<#{$&}>>#{$''}" else "no match" end end Have you defined that method before using it? -- -- Tom Mornini
Ignore that last message. Stupid email client! :) Craig White wrote:> I am furiously trying to find what I am looking for in Pickaxe book and > not finding it. > > I''m getting some values back from a posted form and I need to get the > ''id'' number off the backend. The ''String'' will always come back to me > as... > > Some_name\r\n123Right... where was I? Oh, yeah. The returned value is a multi-line field. But that''s not going to be a problem in this case because getting the second line is really quite simple.> where 123 is actually the id number which I need to execute the find. > The reason they are coming back this way is from a composite > ''text_auto_complete_for'' methodology which is allowing users to pick by > name and I want the associated id column for an easy find. > > But I can''t get to square one with irb... > > irb(main):021:0> myvar = "Some_name\r\n123" > => "Some_name\r\n123" > irb(main):022:0> show_regexp(myvar, /\w+/) > NoMethodError: undefined method `show_regexp'' for main:Object > from (irb):22 > from :0You can''t use show_regexp like that unless you''ve recently defined it as a stand alone method. That''s why irb is telling you it''s undefined.> Please someone... > > a. tell me what type of command I would need to pick the numbers off the > endGive this a try: id_num = "Some_name\r\n123".split("\r\n")[1]> b. tell me why irb doesn''t do something so obviously in the Pickaxe > book.I don''t really have an answer on that, but from a Ruby standpoint, I''d say you were making an incorrect attempt at calling a method. I''ve never looked at show_regexp, but I''d guess it''s a String or Regex object method, so you would need to treat it as such, if you want to have it return a usable value. -Brian
Brian V. Hughes wrote:> Craig White wrote: >> >> Some_name\r\n123 >> >> a. tell me what type of command I would need to pick the numbers off the >> end > > Give this a try: id_num = "Some_name\r\n123".split("\r\n")[1]I''d make one quick ammendment to my statement above: id_num = "Some_name\r\n123".split("\r\n")[1].to_i That way, you know you''re getting back a Fixnum value, not a string. -Brian
Hi Craig, here is an alternative to doing something like that, this relies on this patch which I hope gets included: Patch: http://dev.rubyonrails.org/ticket/3691 In the view, your typical auto_complete, note the hidden field for the id, and the javascript callback (after_update_element): <%= text_field_tag ''client'', '''' %> <%= hidden_field ''encounter'', ''client_id'' %> <div id="client_auto_complete" class="auto_complete"></div> <%= auto_complete_field ''client'', :url => {:controller => ''clients'', :action => ''auto_complete_for_client''}, :after_update_element => ''auto_complete_on_select'', :select => ''name'' %> Also in your view, in a script block. Here I use the DOM to grab the ID: function auto_complete_on_select(element, selectedElement) { document.getElementById(''encounter_client_id'').value selectedElement.childNodes.item(0).innerHTML; } And finally, the partial for the autocomplete: <ul class="clients"> <% for client in @clients do -%> <li class="client"> <span class="informal"><%= client.id -%></span> <div class="name"><%= h client.name %></div> </li> <% end -%> </ul> The informal hides the ID element from view and the javascript uses that to populate the hidden field, so that your ID is available when you submit the form. To answer your original question: irb(main):019:0> var = "Some_name\r\n123" => "Some_name\r\n123" irb(main):020:0> var =~ /([[:digit:]+])/m => 11 For the show_regexp, do you have the function defined? Its not a ruby function. def show_regexp(a, re) if a =~ re "#{$`}<<#{$&}>>#{$''}" else "no match" end end Bob Silva http://www.railtie.net/> -----Original Message----- > From: rails-bounces@lists.rubyonrails.org [mailto:rails- > bounces@lists.rubyonrails.org] On Behalf Of Craig White > Sent: Wednesday, March 08, 2006 7:48 PM > To: rails@lists.rubyonrails.org > Subject: [Rails] regular expressions slay me > > I am furiously trying to find what I am looking for in Pickaxe book and > not finding it. > > I''m getting some values back from a posted form and I need to get the > ''id'' number off the backend. The ''String'' will always come back to me > as... > > Some_name\r\n123 > > where 123 is actually the id number which I need to execute the find. > The reason they are coming back this way is from a composite > ''text_auto_complete_for'' methodology which is allowing users to pick by > name and I want the associated id column for an easy find. > > But I can''t get to square one with irb... > > irb(main):021:0> myvar = "Some_name\r\n123" > => "Some_name\r\n123" > irb(main):022:0> show_regexp(myvar, /\w+/) > NoMethodError: undefined method `show_regexp'' for main:Object > from (irb):22 > from :0 > > Please someone... > > a. tell me what type of command I would need to pick the numbers off the > end > > b. tell me why irb doesn''t do something so obviously in the Pickaxe > book. > > Thanks > > Craig > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Hi -- On Wed, 8 Mar 2006, Craig White wrote:> I am furiously trying to find what I am looking for in Pickaxe book and > not finding it. > > I''m getting some values back from a posted form and I need to get the > ''id'' number off the backend. The ''String'' will always come back to me > as... > > Some_name\r\n123 > > where 123 is actually the id number which I need to execute the find. > The reason they are coming back this way is from a composite > ''text_auto_complete_for'' methodology which is allowing users to pick by > name and I want the associated id column for an easy find. > > But I can''t get to square one with irb... > > irb(main):021:0> myvar = "Some_name\r\n123" > => "Some_name\r\n123" > irb(main):022:0> show_regexp(myvar, /\w+/) > NoMethodError: undefined method `show_regexp'' for main:Object > from (irb):22 > from :0 > > Please someone... > > a. tell me what type of command I would need to pick the numbers off the > endIf those are definitely the only numbers in the string, you could do: numbers = myvar[/\d+/] meaning: the substring of myvar that consists of one-or-more digits (\d+). If you need to specify the digits at the end of the entire string (as opposed to any possible other digits), you would use an "anchor" in the regular expression, to indicate end-of-string: numbers = myvar[/\d+\Z/] The \Z anchor actually means: end of string, give or take a trailing \n. \z means absolute end of string. \Z is preferable if you''re not sure whether there''s a trailing \n and don''t want to worry about it.> b. tell me why irb doesn''t do something so obviously in the Pickaxe > book.I can''t put my finger on show_regexp in the Pickaxe but I think the answer is that it''s defined there. It''s not part of Ruby; that''s why irb doesn''t know about it :-) David -- David A. Black (dblack@wobblini.net) Ruby Power and Light, LLC (http://www.rubypowerandlight.com) "Ruby for Rails" chapters now available from Manning Early Access Program! http://www.manning.com/books/black
On Wed, 2006-03-08 at 19:59 -0800, Tom Mornini wrote:> On Mar 8, 2006, at 7:48 PM, Craig White wrote: > > > I am furiously trying to find what I am looking for in Pickaxe book > > and > > not finding it. > > > > I''m getting some values back from a posted form and I need to get the > > ''id'' number off the backend. The ''String'' will always come back to me > > as... > > > > Some_name\r\n123 > > > > where 123 is actually the id number which I need to execute the find. > > The reason they are coming back this way is from a composite > > ''text_auto_complete_for'' methodology which is allowing users to > > pick by > > name and I want the associated id column for an easy find. > > > > But I can''t get to square one with irb... > > > > irb(main):021:0> myvar = "Some_name\r\n123" > > => "Some_name\r\n123" > > irb(main):022:0> show_regexp(myvar, /\w+/) > > NoMethodError: undefined method `show_regexp'' for main:Object > > from (irb):22 > > from :0 > > > > Please someone... > > > > a. tell me what type of command I would need to pick the numbers > > off the > > end > > /(\d+)$/ > > Answer in $1 > > > b. tell me why irb doesn''t do something so obviously in the Pickaxe > > book. > > Page 93 of the Pick Axe shows give this for show_regex... > > def show_regexp(a, re) > if a =~ re > "#{$`}<<#{$&}>>#{$''}" > else > "no match" > end > end > > Have you defined that method before using it?---- No - I''m looking at page 73 and I''m seeing the examples and thinking, heck, they work there... I hadn''t gotten to p. 93 yet ;-) Thanks...between the two answers, I can probably make it work but you can understand my desire to use irb to test these things out so I don''t have to bother the list. Craig
I''m gonna have to take some time to digest this...interesting that you come around with this now that I have all of the puzzle solved. This one was a several week puzzle. It starts with aggregation fields, using the aggregation for auto_complete and finally, I was able to get the id # along for the ride because finding on aggregation fields can be a problem. I can do it the ''rails'' way, but I''ve been spoiled by Ezra''s ez_where plug-in which tremendously simplify complex SQL finds. The only limitation I have run into with Ezra''s ez_where plug-in is including a series of concatenated columns in a postgres table and now I can finally end run around that limitation (and I say limitation but I doubt that what I am doing is commonly done with SQL finds). Anyway, thanks and I''m going to investigate it. Craig On Wed, 2006-03-08 at 20:02 -0800, Bob Silva wrote:> Hi Craig, here is an alternative to doing something like that, this relies > on this patch which I hope gets included: > > Patch: http://dev.rubyonrails.org/ticket/3691 > > > In the view, your typical auto_complete, note the hidden field for the id, > and the javascript callback (after_update_element): > > <%= text_field_tag ''client'', '''' %> > <%= hidden_field ''encounter'', ''client_id'' %> > <div id="client_auto_complete" class="auto_complete"></div> > <%= auto_complete_field ''client'', :url => {:controller => ''clients'', :action > => ''auto_complete_for_client''}, :after_update_element => > ''auto_complete_on_select'', :select => ''name'' %> > > > Also in your view, in a script block. Here I use the DOM to grab the ID: > > function auto_complete_on_select(element, selectedElement) > { > document.getElementById(''encounter_client_id'').value > selectedElement.childNodes.item(0).innerHTML; > } > > > And finally, the partial for the autocomplete: > > <ul class="clients"> > <% for client in @clients do -%> > <li class="client"> > <span class="informal"><%= client.id -%></span> > <div class="name"><%= h client.name %></div> > </li> > <% end -%> > </ul> > > The informal hides the ID element from view and the javascript uses that to > populate the hidden field, so that your ID is available when you submit the > form. > > To answer your original question: > > irb(main):019:0> var = "Some_name\r\n123" > => "Some_name\r\n123" > irb(main):020:0> var =~ /([[:digit:]+])/m > => 11 > > For the show_regexp, do you have the function defined? Its not a ruby > function. > > def show_regexp(a, re) > if a =~ re > "#{$`}<<#{$&}>>#{$''}" > else > "no match" > end > end > > > Bob Silva > http://www.railtie.net/ > > > -----Original Message----- > > From: rails-bounces@lists.rubyonrails.org [mailto:rails- > > bounces@lists.rubyonrails.org] On Behalf Of Craig White > > Sent: Wednesday, March 08, 2006 7:48 PM > > To: rails@lists.rubyonrails.org > > Subject: [Rails] regular expressions slay me > > > > I am furiously trying to find what I am looking for in Pickaxe book and > > not finding it. > > > > I''m getting some values back from a posted form and I need to get the > > ''id'' number off the backend. The ''String'' will always come back to me > > as... > > > > Some_name\r\n123 > > > > where 123 is actually the id number which I need to execute the find. > > The reason they are coming back this way is from a composite > > ''text_auto_complete_for'' methodology which is allowing users to pick by > > name and I want the associated id column for an easy find. > > > > But I can''t get to square one with irb... > > > > irb(main):021:0> myvar = "Some_name\r\n123" > > => "Some_name\r\n123" > > irb(main):022:0> show_regexp(myvar, /\w+/) > > NoMethodError: undefined method `show_regexp'' for main:Object > > from (irb):22 > > from :0 > > > > Please someone... > > > > a. tell me what type of command I would need to pick the numbers off the > > end > > > > b. tell me why irb doesn''t do something so obviously in the Pickaxe > > book. > > > > Thanks > > > > Craig > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
On Wed, 2006-03-08 at 23:02 -0500, Brian V. Hughes wrote:> Brian V. Hughes wrote: > > Craig White wrote: > >> > >> Some_name\r\n123 > >> > >> a. tell me what type of command I would need to pick the numbers off the > >> end > > > > Give this a try: id_num = "Some_name\r\n123".split("\r\n")[1] > > I''d make one quick ammendment to my statement above: > > id_num = "Some_name\r\n123".split("\r\n")[1].to_i > > That way, you know you''re getting back a Fixnum value, not a string.---- the first one slayed my dragon...I got the concept though thanks Craig
On Wed, 2006-03-08 at 20:04 -0800, dblack@wobblini.net wrote:> Hi -- > > On Wed, 8 Mar 2006, Craig White wrote: > > > I am furiously trying to find what I am looking for in Pickaxe book and > > not finding it. > > > > I''m getting some values back from a posted form and I need to get the > > ''id'' number off the backend. The ''String'' will always come back to me > > as... > > > > Some_name\r\n123 > > > > where 123 is actually the id number which I need to execute the find. > > The reason they are coming back this way is from a composite > > ''text_auto_complete_for'' methodology which is allowing users to pick by > > name and I want the associated id column for an easy find. > > > > But I can''t get to square one with irb... > > > > irb(main):021:0> myvar = "Some_name\r\n123" > > => "Some_name\r\n123" > > irb(main):022:0> show_regexp(myvar, /\w+/) > > NoMethodError: undefined method `show_regexp'' for main:Object > > from (irb):22 > > from :0 > > > > Please someone... > > > > a. tell me what type of command I would need to pick the numbers off the > > end > > If those are definitely the only numbers in the string, you could do: > > numbers = myvar[/\d+/] > > meaning: the substring of myvar that consists of one-or-more digits > (\d+). > > If you need to specify the digits at the end of the entire string (as > opposed to any possible other digits), you would use an "anchor" in > the regular expression, to indicate end-of-string: > > numbers = myvar[/\d+\Z/] > > The \Z anchor actually means: end of string, give or take a trailing > \n. \z means absolute end of string. \Z is preferable if you''re not > sure whether there''s a trailing \n and don''t want to worry about it. > > > b. tell me why irb doesn''t do something so obviously in the Pickaxe > > book. > > I can''t put my finger on show_regexp in the Pickaxe but I think the > answer is that it''s defined there. It''s not part of Ruby; that''s why > irb doesn''t know about it :-)---- thanks David - this actually gave me a clearer idea of the methodology. I probably have to start digging in at the beginning of the chapter of the pickaxe book to figure out why he''s giving examples that won''t work in irb - and as you and others have pointed out, what I assumed to be a ruby command...show_regexp is not a ruby command at all and would have to be some kind of method that I hadn''t created. I did read it once linearly and try to use it as reference but of course not in a linear fashion and this time it frustrated me. I hope that you appreciate that I wanted to work through the regular expressions and figured irb was the lab to do that but there are times when I don''t have what I need in irb at my disposal. Craig
On 3/8/06, Craig White <craigwhite@azapple.com> wrote:> I hope that you appreciate that I wanted to work through the regular > expressions and figured irb was the lab to do that but there are times > when I don''t have what I need in irb at my disposal.On a related note, "Mastering Regular Expressions" by Jeffrey Friedl is easily one of the best software-related books I''ve ever read. http://www.oreilly.com/catalog/regex2/ -- James
On Thu, 2006-03-09 at 23:25 -0600, James Ludlow wrote:> On 3/8/06, Craig White <craigwhite@azapple.com> wrote: > > I hope that you appreciate that I wanted to work through the regular > > expressions and figured irb was the lab to do that but there are times > > when I don''t have what I need in irb at my disposal. > > On a related note, "Mastering Regular Expressions" by Jeffrey Friedl > is easily one of the best software-related books I''ve ever read. > > http://www.oreilly.com/catalog/regex2/---- thanks, I''ll file that in books that I should read but don''t want to read (like Dave''s subversion book and many others) because I don''t use it enough to want to be expert at it...at most, it''s a once in a great while thing. My issue was getting the ability to test within irb and it was pointed out to me that somewhere earlier in the chapter, I undoubtedly missed a ''method'' that made the samples throughout the chapter work and I clearly wasn''t working the book in a linear fashion. Craig
Also check out one of the best interactive regexp gui tools... http://www.weitz.de/regex-coach/ -bakki On 3/10/06, James Ludlow <jamesludlow@gmail.com> wrote:> On 3/8/06, Craig White <craigwhite@azapple.com> wrote: > > I hope that you appreciate that I wanted to work through the regular > > expressions and figured irb was the lab to do that but there are times > > when I don''t have what I need in irb at my disposal. > > On a related note, "Mastering Regular Expressions" by Jeffrey Friedl > is easily one of the best software-related books I''ve ever read. > > http://www.oreilly.com/catalog/regex2/ > > > -- James > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
You can also check out - http://www.sellsbrothers.com/tools/#regexd (nice gui tools for designing regexp) - http://www.regular-expressions.info/ (good site with tutorials) hth Thibaut -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060310/4f379fe6/attachment.html