开发者

Ruby String parsing like how rails parses routes (with :symbols) [duplicate]

开发者 https://www.devze.com 2023-01-06 03:17 出处:网络
This question already has answers here: Closed 10 years ago. Possible Duplicate: Path parsing in rails I\'m trying to parse out some log files with ruby.I have plenty of experience with
This question already has answers here: Closed 10 years ago.

Possible Duplicate:

Path parsing in rails

I'm trying to parse out some log files with ruby. I have plenty of experience with regexes but the other day I was using rails routes and thought that might be a really neat way to parse out these lo开发者_运维问答g files because it would be incredibly easy to understand the parser. Regex, on the other hand, takes several minutes for most people to digest into understandable logic.

The "black magic" of Rails seems to do things like this:

#some sort of route (the format string):
map.awesome_route 'awesome/:id/:slug.:format'
#
#presumably behind the scenes:
url = 'stackoverflow.com/awesome/3/all_my_questions.xml
hash = url.parse_by_route map.awesome_route, url

#now, again presumably:
#
#hash[:id] => '3'
#hash[:slug] => 'all_my_questions'
#hash[:format] => 'xml'

Maybe this isn't the way Rails actually does things. But it should. Is there a way to parse out my log file like that? It would really really really make my day.

Thanks!


maybe ActionController::Routing::Routes.recognize_path is what you're looking for?

ActionController::Routing::Routes.recognize_path '/users/6'
# => { :controller => 'users', :action => 'show', :id => 6 }

If you want to parse logfiles in a script, it shouldn't be a problem to parse your routes.rb before starting, so that the routes mapping will be set up.

0

精彩评论

暂无评论...
验证码 换一张
取 消