# How to create a mapping of basic Types (Int32, String, Int8, etc) to their conversion methods?

**URL:** <https://forum.crystal-lang.org/t/how-to-create-a-mapping-of-basic-types-int32-string-int8-etc-to-their-conversion-methods/1007>\
**Category:** Help & Support\
**Created:** [July 29, 2019, 6:13am UTC](https://forum.crystal-lang.org/t/how-to-create-a-mapping-of-basic-types-int32-string-int8-etc-to-their-conversion-methods/1007 "2019-07-29T06:13:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![girng](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/girng/32/127_2.png) [@girng](https://forum.crystal-lang.org/u/girng)\
**Post date:** [July 29, 2019, 6:13am UTC](https://forum.crystal-lang.org/t/how-to-create-a-mapping-of-basic-types-int32-string-int8-etc-to-their-conversion-methods/1007/1 "2019-07-29T06:13:04Z")

</div>

For example: `Int32`'s conversion method is `to_i`.  
`String` is `to_s`, and so on.

```auto
# Please no code shaming (all this is just a contrived example)

test_string = "5000"

def extract_example(test_string, **t)
  pp t[:custom_key]
  case t[:custom_key]
  	when Int32
  		test_string = test_string.to_i
  		puts "I can't get this to call..."
	end
  test_string
end

pp extract_example(test_string, custom_key: Int32)

```

Please no macros (if possible)

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [July 29, 2019, 7:18am UTC](https://forum.crystal-lang.org/t/how-to-create-a-mapping-of-basic-types-int32-string-int8-etc-to-their-conversion-methods/1007/2 "2019-07-29T07:18:18Z")

</div>

Every number type has a `.new` method accepting a string, which in turn will invoke the respective `String#to_x` method.

---

<div class="post-metadata">

**Author:** ![girng](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/girng/32/127_2.png) [@girng](https://forum.crystal-lang.org/u/girng)\
**Post date:** [July 29, 2019, 7:21am UTC](https://forum.crystal-lang.org/t/how-to-create-a-mapping-of-basic-types-int32-string-int8-etc-to-their-conversion-methods/1007/3 "2019-07-29T07:21:06Z")

</div>

[https://play.crystal-lang.org/#/r/7bho](https://play.crystal-lang.org/#/r/7bho)

```auto
def example(string, **t)
  new_value = t[:selected_type].new(string)
  
  p! new_value
  p! typeof(new_value)
end

example("5000", selected_type: Int32)

```

AWESOME! Thanks @straight-shoota

---

<div class="post-metadata">

**Author:** ![watzon](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/watzon/32/481_2.png) [@watzon](https://forum.crystal-lang.org/u/watzon)\
**Post date:** [July 29, 2019, 7:22am UTC](https://forum.crystal-lang.org/t/how-to-create-a-mapping-of-basic-types-int32-string-int8-etc-to-their-conversion-methods/1007/4 "2019-07-29T07:22:16Z")

</div>

Yeah I actually made use of that in [Apatite](https://github.com/watzon/apatite/blob/master/src/apatite/linear_algebra/matrix.cr#L158). Very helpful.
