Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

uninitialized constant bigdecimal (nameerror)

Tags:

ruby

val = BigDecimal.new("0")

Running this statement shows uninitialized constant bigdecimal (nameerror) error.

Then modifying to:

require "bigdecimal"
val = BigDecimal.new("0")

That's ok. BigDecimal belongs to Ruby Standard Library. So I need to require 'bigdecimal'?

Question:

  • Where is BigDecimal defined?
  • What's the definition file? I didn't find the file in lib folder.
like image 538
niaomingjian Avatar asked Aug 04 '26 01:08

niaomingjian


1 Answers

Just because it's in the Ruby core doesn't mean it's automatically imported into your program. If it loaded everything it'd take forever to get Ruby started.

The correct way to do this is to use require. Why? It's only loaded if you need it.

The reason you can't find bigdecimal.rb is because it's written in C for performance reasons, so it's a library that's actually imported on-demand.

like image 59
tadman Avatar answered Aug 06 '26 14:08

tadman