This post originated from an RSS feed registered with Ruby Buzz
by Red Handed.
Original Post: The Slice Matcher
Feed Title: RedHanded
Feed URL: http://redhanded.hobix.com/index.xml
Feed Description: sneaking Ruby through the system
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Red Handed
Latest Posts From RedHanded
Advertisement
String#[]
and slice
both hand you back matches, right?
>> "redhanded.hobix.com:80"[/:\d+$/]
=> ":80"
>> "Matching Mole -- Little Red Record".slice(/Mo\w+/)
=> "Mole"
But it’s no good for getting back match groups.
>> "02. One of Us is Dead.mp3"[/^\d+\. (.*)\.mp3$/]
=> "02. One of Us is Dead.mp3"
Oh, no, wait, it is so good at it!
>> "02. One of Us is Dead.mp3"[/^\d+\. (.*)\.mp3$/, 1]
=> "One of Us is Dead"
Credit due: Found this while admiring flgr’s one-line fantastic web server he left in the comments several months ago:
require'socket';s=TCPServer.new 80;loop{c=s.accept;c<<"\n"+IO.read(
c.gets[/\/(.*) /,1].gsub(/\.{2,}/){})rescue 404;c.close}
Read: The Slice Matcher