Skip to content

(19864) num2bool match fix #139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 1, 2013

Conversation

hakamadare
Copy link
Contributor

This is another attempt at #137.

This is a bit more heavy-handed than I might like, but it does appear to do the right things:

  • accepts numeric input appropriately, truncating floats
  • matches string input against a regex, then coerces number-looking strings to int
  • makes a best effort to coerce anything else to a string, then subjects it to the same treatment
  • raises an error in the event of incorrect number of arguments or non-number-looking strings

I've also included some additional unit tests.

Also ignore rspec fixtures directory
No more coercing to String and regex matching.  Instead, we now coerce
to Integer at the beginning or raise an error if we cannot coerce to
Integer.

A consequence of this change is that the function will now accept
blatantly non-numeric strings as input, and return false.  This seems a
bit goofy to me, but it's how String#to_i works.  If we really don't
like this, then I'm open to suggestions.
This is a bit more heavy-handed than I might like, but it does appear to
do the right things:

* accepts numeric input appropriately, truncating floats
* matches string input against a regex, then coerces number-looking
  strings to int
* makes a best effort to coerce anything else to a string, then subjects
  it to the same treatment
* raises an error in the event of incorrect number of arguments or
  non-number-looking strings

I've also included some additional unit tests.
@hakamadare
Copy link
Contributor Author

Um, ok, now I'm even more confused. The Travis build succeeded for Puppet 3 and Puppet 2.7 on Ruby 1.8.7 (which is what I use), but failed on Ruby 1.9.3. Off the top of my head I have no idea what's going on there; any suggestions?

number = number.to_i

# Return true for any positive number and false otherwise
return number > 0 ? true : false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

number > 0 will return a boolean, so you don't need to use the ternary operator here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's straightforward enough; fixed.

@adrienthebo
Copy link
Contributor

@hakamadare I initially thought it was an issue with the regex, and that using Float() might fix it (although it might be a good change regardless). However it looks like you're passing an array within an array which is leading to the spec failures.

* use Float() to process string arguments
* get rid of doubly nested arrays
* removing needless ternary operator
* improving error message handling
adrienthebo added a commit that referenced this pull request Apr 1, 2013
@adrienthebo adrienthebo merged commit f90c54e into puppetlabs:master Apr 1, 2013
@adrienthebo
Copy link
Contributor

Merged into master as f90c54e.

This should be released in the next major release of stdlib.

Thanks again for the contribution!

-Adrien

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants