6 Replies - 6044 Views - Last Post: 03 October 2012 - 06:06 PM Rate Topic: -----

#1 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 902
  • View blog
  • Posts: 3,163
  • Joined: 12-May 09

Ruby Code Golf: Grep

Post icon  Posted 03 October 2012 - 07:00 AM

Just a simple challenge to get back in the swing of things - life has been crazy. Of course, if my excuse is always that "life is crazy," I should just deal with it and call that "normal."

Right, the challenge.
In the fewest characters possible, implement grep. This version of grep is called as follows:
grep.rb regexp [File(s)]


If no files are present, the script should read from stdin. No requirements to support the various switches/flags, just that the script outputs lines matching the regexp.

Support Ruby regular expressions, rather than grep.

Restrictions:
1) No system calls of any kind
2) You shouldn't have to "require" or "load" anything
3) The results should be printed to STDOUT, one line of output per matching string.

This post has been edited by xclite: 03 October 2012 - 07:12 AM


Is This A Good Question/Topic? 2
  • +

Replies To: Ruby Code Golf: Grep

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2101
  • View blog
  • Posts: 3,203
  • Joined: 21-June 11

Re: Ruby Code Golf: Grep

Posted 03 October 2012 - 07:08 AM

Should the supported regex syntax be that of Ruby regexen or the one supported by grep?

Also: are there any restrictions on built-in methods that can be used?

This post has been edited by sepp2k: 03 October 2012 - 07:09 AM

Was This Post Helpful? 1
  • +
  • -

#3 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 902
  • View blog
  • Posts: 3,163
  • Joined: 12-May 09

Re: Ruby Code Golf: Grep

Posted 03 October 2012 - 07:12 AM

Built-in methods are allowed, and I'm aware there are a few that will make this quite simple.

And I was going to mention which version but I totally forgot to include it: just use it as a Ruby REGEX instead of supporting grep.
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2101
  • View blog
  • Posts: 3,203
  • Joined: 21-June 11

Re: Ruby Code Golf: Grep

Posted 03 October 2012 - 07:19 AM

Okay, here's my solution - 25 characters:

Spoiler

Was This Post Helpful? 4
  • +
  • -

#5 Lemur  Icon User is offline

  • Pragmatism over Dogma
  • member icon


Reputation: 1359
  • View blog
  • Posts: 3,425
  • Joined: 28-November 09

Re: Ruby Code Golf: Grep

Posted 03 October 2012 - 08:09 AM

View Postsepp2k, on 03 October 2012 - 09:19 AM, said:

Okay, here's my solution - 25 characters:

Spoiler



Can't get much shorter than that one.
Was This Post Helpful? 0
  • +
  • -

#6 NotarySojac  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 428
  • Joined: 30-September 10

Re: Ruby Code Golf: Grep

Posted 03 October 2012 - 05:16 PM

In ruby, what does dollar sign less than symbol mean? I got most of that solusion, but that first symbol is evading google.

Here's an expanded solution that makes the algo slightly less cryptic, to anyone else who got a little lost:
Spoiler

This post has been edited by NotarySojac: 03 October 2012 - 06:41 PM

Was This Post Helpful? 0
  • +
  • -

#7 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1152
  • View blog
  • Posts: 2,530
  • Joined: 05-May 05

Re: Ruby Code Golf: Grep

Posted 03 October 2012 - 06:06 PM

Quote

Okay, here's my solution - 25 characters:


Very Impressive.

Quote

In ruby, what does dollar sign less than symbol mean? I got most of that solusion, but that first symbol is evading google.


No need for him to explain it again.

Here's mine: 149 characters.

Spoiler


I need to DRY that out.

This post has been edited by blackcompe: 03 October 2012 - 06:36 PM

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1