Ruby Quiz - Challenge #7 - Type Inference - Convert Strings to Null, Number, Not a Number (NaN), Date & More

Gerald Bauer <[email protected]>
Newsgroups gmane.comp.lang.ruby.general
Message-ID <CAAxEZd9A-1gzdOqcS_2TkLir6hTWS7n4Y-c0bU6VCAfzw1S2vQ@mail.gmail.com>
Hello,

  It's Friday. Ruby Quiz time! Join us. Let's keep going with a new
Ruby Quiz [1] every Friday. Here we go:

Challenge #7 - Type Inference - Convert Strings to Null, Number, Not a
Number (NaN), Date & More

Type inference is the new hotness.
Let's (auto)-convert a list of strings into properly typed values.

The challenge: Code a `convert` method that passes the RubyQuizTest :-) [2].

    def convert( values )
      # ...
    end

For the starter level 1 turn a random list of strings:

    ["2018", "2018.12", "2018.12.25", "25 Gems", "NaN", "1820",
"18.20", "Nil", "Null"]


into a properly typed list of values:

    [2018, 2018.12, Date.new(2018,12,25), "25 Gems", Float::NAN, 1820,
18.2, nil, nil]
    # => [2018, 2018.12, #<Date: 2018-12-25>, "25 Gems", NaN, 1820,
18.2, nil, nil]

Note: `NaN` is short for Not a Number (that is, `Float::NAN`).
To pass the RubyQuizTest all type classes must match too, that is:

    [Integer, Float, Date, String, Float, Integer, Float, NilClass, NilClass]

To make sure the order in the list doesn't matter, that is, integer
before float numbers
or float before integer numbers or date before integer numbers
and so on -
convert gets called five times
with a shuffled sample with a different "randomized" order every time.


Start from scratch or, yes, use any library / gem you can find.

To qualify for solving the code challenge / puzzle you must pass the test:

```
require 'minitest/autorun'
require 'date'

class RubyQuizTest < MiniTest::Test

  def test_convert

    pairs = [
      ["2018",       2018],
      ["2018.12",    2018.12],
      ["2018.12.25", Date.new(2018,12,25)],
      ["25 Gems",    "25 Gems"],
      ["NaN",        Float::NAN],
      ["1820",       1820],
      ["18.20",      18.2],
      ["Nil",        nil],
      ["Null",       nil]]

    5.times do
      values, exp_values = pairs.shuffle.reduce( [[],[]]) { |acc,pair|
acc[0] << pair[0]; acc[1] << pair[1]; acc }

      assert_equal exp_values, convert( values )
      assert_equal exp_values.map { |v| v.class }, convert( values
).map { |v| v.class }
    end


  end # method test_convert
end # class RubyQuizTest
```

Post your code snippets on the "official" Ruby Quiz Channel,
that is, the ruby-talk mailing list right here.

Happy data wrangling and type inferencing with Ruby.


[1] https://github.com/planetruby/quiz/tree/master/007
[2] https://github.com/planetruby/quiz/blob/master/007/test.rb

Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.