Crystal: to_big_i doesn't work out of the box

Created on 20 Jun 2018  路  6Comments  路  Source: crystal-lang/crystal

I was trying to run the following code:

puts "3a060dbf8d1a5ac3e67bc8f18843fc48".to_i(16)

This throws an error "Invalid Int32".
After some digging around in the String doc page I found the method I needed was to_big_i(16).
Like this:

puts "3a060dbf8d1a5ac3e67bc8f18843fc48".to_big_i(16)

but this throws "Undefined method to_big_i for String". I later found out I had to require the big package because that is where the string method is defined. So this is the working example:

require "big"
puts "3a060dbf8d1a5ac3e67bc8f18843fc48".to_big_i(16)

It makes sense but looking at the docs page it is unclear that I needed to require the "big" module.

I'm sure this happens with other packages because not everything is included in the prelude.cr file.
I think the docs should inform when a method is defined in a different file/module that isn't included by default.

crystal -v
Crystal 0.25.0 (2018-06-15)

LLVM: 5.0.1
Default target: x86_64-apple-macosx
help-wanted docs feature

Most helpful comment

Given the nature of how modules / files are defined, either

  1. the code samples need to have the minimum required statement, or
  2. we should annotate the methods (or the type) with which file should be required and show that information in each method while generating docs.

All 6 comments

Given the nature of how modules / files are defined, either

  1. the code samples need to have the minimum required statement, or
  2. we should annotate the methods (or the type) with which file should be required and show that information in each method while generating docs.

1) would be the simplest, and possible to do right now.

I think one solution doesn't exclude the other, having a good code sample in my opinion makes it a lot easier for whoever's reading the docs.

  1. Seems like a nice to have but a bit hard to implement.
    Can I open up a PR with docs with code samples in ?

yes, please do open a PR!

Another thing to do, "Invalid Int32" is a bit of a nondescriptive message, we should mention that the number is too large/would overflow.

This can be closed?

Yes, let's close this.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

malte-v picture malte-v  路  77Comments

sergey-kucher picture sergey-kucher  路  66Comments

fridgerator picture fridgerator  路  79Comments

stugol picture stugol  路  70Comments

asterite picture asterite  路  60Comments