Live data from Hacker News

What are your Ruby Regex Idioms?

blog.samstokes.co.uk

11–20 of 24 posts

Re: What are your Ruby Regex Idioms?

#11
post #4

Woah.. I want that for Python..

I sure don’t. Putting a regular expression in a slice for a string runs totally counter to all existing python idioms, and would utterly confuse anyone trying to read the code. There are definitely other ways of accomplishing this with reasonably compact syntax that are also intuitive and clear. (But I'm unconvinced it needs special syntax at all. It saves a couple lines here and there, maybe, but at the cost of maki…

Your code does not accomplish the required task. The goal is to return nil when there is no match, or the nth match if there is.

  >>> re.search('a(x)a', 'axa').group(1)
  'x'
  >>> re.search('a(x)a', 'aya').group(1)
  Traceback (most recent call last):
  File "", line 1, in 
  AttributeError: 'NoneType' object has no attribute 'group'
The idiomatic python for the job is:

  match = re.search("`([^']*)'", caller[0])
  if match:
     match.group(1)
  else:
     None
(or some nonsense involving try/except, which would be even worse).

Re: What are your Ruby Regex Idioms?

#12
post #11

Earlier quoted context omitted.

I sure don’t. Putting a regular expression in a slice for a string runs totally counter to all existing python idioms, and would utterly confuse anyone trying to read the code. There are definitely other ways of accomplishing this with reasonably compact syntax that are also intuitive and clear. (But I'm unconvinced it needs special syntax at all. It saves a couple lines here and there, maybe, but at the cost of maki…

Your code does not accomplish the required task. The goal is to return nil when there is no match, or the nth match if there is. >>> re.search('a(x)a', 'axa').group(1) 'x' >>> re.search('a(x)a', 'aya').group(1) Traceback (most recent call last): File " ", line 1, in AttributeError: 'NoneType' object has no attribute 'group' The idiomatic python for the job is: match = re.search("`([^']*)'", caller[0]) if match: match…

Ah, you’re right. In many cases, I don’t want to return None when there’s no match, but for short scripts quickly tossed together, the extra few lines can be a bit annoying.

  try: re.search("`([^']*)'", caller[0]).group(1)
  except: None

Re: What are your Ruby Regex Idioms?

#13

I use Rubular all the time for testing my regexes with ease. Give it a shot, its really well done: http://rubular.com/

Looks like a useful tool. I've been using a similar one: http://regex.powertoy.org/

The latter supports several languages' regex dialects, and I like the visual way it displays group matches, particularly when groups are nested. It can be a bit clunky, though, whereas Rubular is pretty clean and usable.

Re: What are your Ruby Regex Idioms?

#15
post #4

Woah.. I want that for Python..

I sure don’t. Putting a regular expression in a slice for a string runs totally counter to all existing python idioms, and would utterly confuse anyone trying to read the code. There are definitely other ways of accomplishing this with reasonably compact syntax that are also intuitive and clear. (But I'm unconvinced it needs special syntax at all. It saves a couple lines here and there, maybe, but at the cost of maki…

> I'm unconvinced it needs special syntax at all.

What's interesting is that it's actually not special syntax - at least, it's not special syntax for regex usage. The syntax obj[arg, arg, arg] looks weird because in most languages things that look like subscripts can't have multiple arguments, but it's just sugar for the method call obj.[](arg, arg, arg), which is pretty mundane. The semantics are just the semantics of that method on the class of 'obj', and can therefore be defined in library or user code.

Note I'm not defending the specific use of it here to write odd-looking regex code - I just think it's interesting that the language is flexible enough to allow that kind of usage without special-casing it.

Raganwald wrote a blog post [1] along similar lines, arguing that it's a core part of Ruby's language philosophy, about this snippet:

    (1..100).inject(&:+)
[1]: http://weblog.raganwald.com/2008/02/1100inject.html

Re: What are your Ruby Regex Idioms?

#16
I didn't see it mentioned, but, btw, this code allows a method to introspect to figure out what the method name that's currently being executed.

caller[0][/`([^']*)'/, 1]

Note that you only need to do this for Ruby 1.8. In Ruby 1.9, there will be a __method__() and __callee__() method that does the same thing.

Re: What are your Ruby Regex Idioms?

#17

Earlier quoted context omitted.

I sure don’t. Putting a regular expression in a slice for a string runs totally counter to all existing python idioms, and would utterly confuse anyone trying to read the code. There are definitely other ways of accomplishing this with reasonably compact syntax that are also intuitive and clear. (But I'm unconvinced it needs special syntax at all. It saves a couple lines here and there, maybe, but at the cost of maki…

> I'm unconvinced it needs special syntax at all. What's interesting is that it's actually not special syntax - at least, it's not special syntax for regex usage. The syntax obj[arg, arg, arg] looks weird because in most languages things that look like subscripts can't have multiple arguments, but it's just sugar for the method call obj.[](arg, arg, arg), which is pretty mundane. The semantics are just the semantics…

[deleted]

Re: What are your Ruby Regex Idioms?

#18

Earlier quoted context omitted.

I sure don’t. Putting a regular expression in a slice for a string runs totally counter to all existing python idioms, and would utterly confuse anyone trying to read the code. There are definitely other ways of accomplishing this with reasonably compact syntax that are also intuitive and clear. (But I'm unconvinced it needs special syntax at all. It saves a couple lines here and there, maybe, but at the cost of maki…

> I'm unconvinced it needs special syntax at all. What's interesting is that it's actually not special syntax - at least, it's not special syntax for regex usage. The syntax obj[arg, arg, arg] looks weird because in most languages things that look like subscripts can't have multiple arguments, but it's just sugar for the method call obj.[](arg, arg, arg), which is pretty mundane. The semantics are just the semantics…

Fair enough. I don’t like the special semantics then, that say that string objects take regular expressions as possible slice indices. It's trying to be too cute, and compactness comes at the direct expense of clarity. Basically the reason I don't like Ruby generally, is that everything tries to be too clever by half, too many behaviors crammed into too few parts. It's sort of halfway from Python to Perl.

Re: What are your Ruby Regex Idioms?

#19

Earlier quoted context omitted.

> I'm unconvinced it needs special syntax at all. What's interesting is that it's actually not special syntax - at least, it's not special syntax for regex usage. The syntax obj[arg, arg, arg] looks weird because in most languages things that look like subscripts can't have multiple arguments, but it's just sugar for the method call obj.[](arg, arg, arg), which is pretty mundane. The semantics are just the semantics…

Fair enough. I don’t like the special semantics then, that say that string objects take regular expressions as possible slice indices. It's trying to be too cute, and compactness comes at the direct expense of clarity. Basically the reason I don't like Ruby generally, is that everything tries to be too clever by half, too many behaviors crammed into too few parts. It's sort of halfway from Python to Perl.

"say that string objects take regular expressions as possible slice indices."

I think it would be more accurate to say that Ruby's String#[] aka String#slice method accepts arguments other than just indices (including regular expressions and other strings) for returning substrings/subpatterns. It's just a different approach, intuitive and rather uncontroversial, IMO.

Post reply on HN