From 59ecc560c21f00dda5d6ccebb16815e585c43f2e Mon Sep 17 00:00:00 2001 From: Piotr Murach Date: Thu, 16 Jul 2020 13:04:10 +0100 Subject: [PATCH] Add docs about array and hash conversions --- README.md | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 80e600ad..b4a3d8d5 100644 --- a/README.md +++ b/README.md @@ -257,6 +257,22 @@ By default no conversion of input is performed. To change this use one of the fo * `:list`|`:array` - e.g. 'a,b,c' becomes `["a", "b", "c"]` * `:map`|`:hash` - e.g. 'a:1 b:2 c:3' becomes `{a: "1", b: "2", c: "3"}` +In addition you can specify a plural or append `list` or `array` to any base type: + +* `:ints` or `:int_list` - will convert to a list of integers +* `:floats` or `:float_list` - will convert to a list of floats +* `:bools` or `:bool_list` - will convert to a list of booleans, e.g. `t,f,t` becomes `[true, false, true]` + +Similarly, you can append `map` or `hash` to any base type: + +* `:int_map`|`:integer_map`|`:int_hash` - will convert to a hash of integers, e.g `a:1 b:2 c:3` becomes `{a: 1, b: 2, c: 3}` +* `:bool_map` | `:boolean_map`|`:bool_hash` - will convert to a hash of booleans, e.g `a:t b:f c:t` becomes `{a: true, b: false, c: true}` + +By default, `map` converts keys to symbols, if you wish to use strings instead specify key type like so: + +* `:str_int_map` - will convert to a hash of string keys and integer values +* `:string_integer_hash` - will convert to a hash of string keys and integer values + For example, if you are interested in range type as answer do the following: ```ruby @@ -265,6 +281,14 @@ prompt.ask("Provide range of numbers?", convert: :range) # => 1..10 ``` +If, on the other hand, you wish to convert input to a hash of integer values do: + +```ruby +prompt.ask("Provide keys and values:", convert: :int_map) +# Provide keys and values: a=1 b=2 c=3 +# => {a: 1, b: 2, c: 3} +``` + If a user provides a wrong type for conversion an error message will be printed in the console: ```ruby @@ -287,11 +311,11 @@ end You can also provide a custom conversion like so: ```ruby -prompt.ask('Ingredients? (comma sep list)') do |q| +prompt.ask("Ingredients? (comma sep list)") do |q| q.convert -> (input) { input.split(/,\s*/) } end # Ingredients? (comma sep list) milk, eggs, flour -# => ['milk', 'eggs', 'flour'] +# => ["milk", "eggs", "flour"] ``` #### 2.1.2 default