Solve a Tricky Ruby Problem and Win a Prize!

Posted on by Tatiana Vasilyeva

Hello everyone,

You know that annoying feeling when you have crutches in your code and can’t seem to find a better solution, don’t you? Well, this is it. We faced a tricky Ruby problem: UTF-8 regular expressions fail when working with non UTF-8 strings. We’ve tried to solve it and improve this chunk of code, but to no avail. We were really stumped. And then we realized this problem could be a good task for a Ruby contest!

So, we’ve prepared the github project with the simple method:

module RegexpM17N
 def self.non_empty?(str)
   str =~ /^.+$/
 end
end

and the test that now fails:
# encoding: utf-8
require 'minitest/autorun'
require_relative '../lib/regexp_m17n'

class RegexpTest < MiniTest::Unit::TestCase
  def test_non_empty_string
    Encoding.list.each do |enc|
      assert(RegexpM17N.non_empty?('.'.encode(enc)))
    end
  end
end

The task is to change the method so the test passes. Do you have any idea how this can be done?

Friday is always a good day to escape from everyday routine and code for your own pleasure. Rise up to the challenge and enter the contest! Just make the test pass and send your pull request before August 10. We have already prepared a nice prize for the best solution:

prize

Learn more on our special webpage or watch this short demo with the instructions.


Have a happy friday!
JetBrains RubyMine Team

Comments below can no longer be edited.

1 Response to Solve a Tricky Ruby Problem and Win a Prize!

  1. Stefan says:

    August 5, 2014

    Not a ruby developer, but I suspect the regex library, maybe using the Oniguruma regexp engine with the /u flag to treat the string as utf8.

    An other dirty trick would be to force convert it to utf8 before runing the regex on

    str

    (.force_encoding("utf-8"))

Subscribe

Subscribe for updates