felipem

felipem

Programming Ruby 3.2 (5th Edition): Description of Integer() should mention it's strictness (638)

@noelrappin

The functionality as described (Convert the argument to the stated type.) for methods like Integer() is very incomplete. It would be nice to highlight how it raises errors if it can’t perform the conversion and that it attempts the conversion in a very strict way. Here are the results of calling to_i or passing them to Integer (respectively) for some example values:

["144", "text", "text123", "14.2", nil].map do |value|
  to_i = value.to_i
  integer = begin
    Integer(value)
  rescue =>  error
    error
  end
  [value, to_i, integer]
end

produces:

[
  ["144", 144, 144],
  ["text", 0, #<ArgumentError: invalid value for Integer(): "text">],
  ["text123", 0, #<ArgumentError: invalid value for Integer(): "text123">],
  ["14.2", 14, #<ArgumentError: invalid value for Integer(): "14.2">],
  [nil, 0, #<TypeError: can't convert nil into Integer>]
]

Where Next?

Popular Pragmatic Bookshelf topics Top

New
edruder
I thought that there might be interest in using the book with Rails 6.1 and Ruby 2.7.2. I’ll note what I needed to do differently here. ...
New
lirux
Hi Jamis, I think there’s an issue with a test on chapter 6. I own the ebook, version P1.0 Feb. 2019. This test doesn’t pass for me: ...
New
mikecargal
Title: Hands-on Rust: question about get_component (page 295) (feel free to respond. “You dug you’re own hole… good luck”) I have somet...
New
raul
Hi Travis! Thank you for the cool book! :slight_smile: I made a list of issues and thought I could post them chapter by chapter. I’m rev...
New
New
s2k
Hi all, currently I wonder how the Tailwind colours work (or don’t work). For example, in app/views/layouts/application.html.erb I have...
New
Keton
When running the program in chapter 8, “Implementing Combat”, the printout Health before attack was never printed so I assumed something ...
New
ggerico
I got this error when executing the plot files on macOS Ventura 13.0.1 with Python 3.10.8 and matplotlib 3.6.1: programming_ML/code/03_...
New
dachristenson
@mfazio23 Android Studio will not accept anything I do when trying to use the Transformations class, as described on pp. 140-141. Googl...
New

Other popular topics Top

AstonJ
A thread that every forum needs! Simply post a link to a track on YouTube (or SoundCloud or Vimeo amongst others!) on a separate line an...
New
Exadra37
I am thinking in building or buy a desktop computer for programing, both professionally and on my free time, and my choice of OS is Linux...
New
New
AstonJ
I have seen the keycaps I want - they are due for a group-buy this week but won’t be delivered until October next year!!! :rofl: The Ser...
New
AstonJ
Do the test and post your score :nerd_face: :keyboard: If possible, please add info such as the keyboard you’re using, the layout (Qw...
New
PragmaticBookshelf
Learn different ways of writing concurrent code in Elixir and increase your application's performance, without sacrificing scalability or...
New
AstonJ
Seems like a lot of people caught it - just wondered whether any of you did? As far as I know I didn’t, but it wouldn’t surprise me if I...
New
New
PragmaticBookshelf
Author Spotlight James Stanier @jstanier James Stanier, author of Effective Remote Work , discusses how to rethink the office as we e...
New
Help
I am trying to crate a game for the Nintendo switch, I wanted to use Java as I am comfortable with that programming language. Can you use...
New

Sub Categories: