Hi Got a column named ''sku'' in a table named ''product'' <%= product.sku %> ...looks like this.... ''1234_56'' These last two chars (56) are an audio Track number. I ultimately want to display: Track <%= product.sku %> I can isolate the first part with /\d+\_/ = 1234_ But can''t figure out how to get the last two chars ...... 56 Can someone point me to a source for that shows how to parse strings like this in a Rails context? THX Chas -- Posted via http://www.ruby-forum.com/.
Many thanks!!! Wouter wrote:> /\_(.+)/ should give you the latest two chars.. If you need those > numbers a lot, I would make a method in your model.. something like: > > def track() > @sku =~ /\_(.+)/ > $1 > end-- Posted via http://www.ruby-forum.com/.
/\_(.+)/ should give you the latest two chars.. If you need those numbers a lot, I would make a method in your model.. something like: def track() @sku =~ /\_(.+)/ $1 end -- Posted via http://www.ruby-forum.com/.
>>>>> "Chas" == Chas Conquest <chas@valley.net> writes:> Can someone point me to a source for that shows how to parse strings > like this in a Rails context?http://www.oreilly.com/catalog/regex2/index.html -- Calle Dybedahl <calle@cyberpomo.com> http://www.livejournal.com/users/cdybedahl/ "Ah, optimism. I remember optimism." -- Anya, Buffy the Vampire Slayer
Thanks Calle, I''m on it..... Wouter.....Could you help me understand this one? How do i call this def track() method? So here is what I think is going on: the (object) "product" has an (attribute) "sku" which happens to have a value associated with "sku" of ''xxxx_xx'' so every time rails finds and displays all these attributes, I want to call: def Track() in the store_controller.rb - run the method and return the regexed value into the display. But i just don''t "get" how to associate the block with def Track.... in the store_controller.rb def track() @sku =~ /\_(.+)/ $1 end in my store/index.rhtml <% product.sku.track do |sku| %> <br/> Track <%= $1 %> <% end %> Thanks for your indulgence! Chas -- Posted via http://www.ruby-forum.com/.
well...I got it a different way. seven characters in a string (ie. ''0123_56'')- I needed the last two as a audio "Track" reference. this works in place without a method or regex component Track <%= product.sku[-2,2] %> # -2 says "start at the right, 2nd place" , 2 says "get 2 characters" all is fine! now I just have to get only 1 character if the first is 0 - ''0123_02'' - get 2 instead of 02 -- Posted via http://www.ruby-forum.com/.
> Track <%= product.sku[-2,2] %> # -2 says "start at the > right, 2nd > place" , 2 says "get 2 characters" > > all is fine! now I just have to get only 1 character if the > first is 0 - > ''0123_02'' - get 2 instead of 02<%= /([1-9]?\d)$/.match(product.sku).to_s %> not sure about it working in .rhtml but irb seems to work fine. There probably a better way to represent it in Ruby but the regex works OK.
Ross Dawson <Ross_Dawson@...> writes:> > > Track <%= product.sku[-2,2] %> # -2 says "start at the > > right, 2nd > > place" , 2 says "get 2 characters" > > > > all is fine! now I just have to get only 1 character if the > > first is 0 - > > ''0123_02'' - get 2 instead of 02 > > <%= /([1-9]?\d)$/.match(product.sku).to_s %> > > not sure about it working in .rhtml but irb seems to work fine. Thereprobably a better way to represent it in> Ruby but the regex works OK. >regex is ok, but i have a simple idea if you dislike regex. product.sku[-2,2].to_i.to_s
On Jun 7, 2006, at 11:00 PM, chenge wrote:> Ross Dawson <Ross_Dawson@...> writes: >>> all is fine! now I just have to get only 1 character if the >>> first is 0 - >>> ''0123_02'' - get 2 instead of 02 >> >> <%= /([1-9]?\d)$/.match(product.sku).to_s %> > > regex is ok, but i have a simple idea if you dislike regex. > > product.sku[-2,2].to_i.to_sDamn! You beat me to it. ;-> That''s exactly what I was going to suggest, since it seems pretty clear that what the OP is trying to get is "the number" that appears at the end of that string, not just the "last two characters, that happen to also be digits." -Brian