Unable to use gems in simple scripts

Hello, i’m just getting started with ruby and have hit a wall attempting
to use some simple gems.

Source:
#! /usr/bin/ruby
require ‘flt’
puts DecNum(22.1138)

thats it!

right now it spits out this error:

ruby -T test.rb
/usr/lib/ruby/1.9.1/rubygems/specification.rb:268:in []': Insecure operation - [] (SecurityError) from /usr/lib/ruby/1.9.1/rubygems/specification.rb:268:inblock in
_all’
from /usr/lib/ruby/1.9.1/rubygems/specification.rb:267:in
reverse_each' from /usr/lib/ruby/1.9.1/rubygems/specification.rb:267:in_all’
from /usr/lib/ruby/1.9.1/rubygems/specification.rb:409:in each' from /usr/lib/ruby/1.9.1/rubygems/specification.rb:441:infind’
from /usr/lib/ruby/1.9.1/rubygems/specification.rb:441:in
find_by_path' from /usr/lib/ruby/1.9.1/rubygems.rb:203:intry_activate’
from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:58:in rescue in require' from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:35:inrequire’
from test.rb:3:in `’

with some configurations i’ve gotten:

ruby -T test.rb
test.rb:7:in <main>': undefined methodDecNum’ for main:Object
(NoMethodError)

i’m using ubuntu 12.04, and installed ruby + rubygems from the
repository, purged it, tried RVM, tried ruby1.9 from the repository’s.
I’ve spent the last 3 days scouring the Internet for a solution, found a
few older bug reports about it the issue, but no answers. 90% of the
hits i get are about security issues in rails applications, which i
really dont care about as i’m not messing with web-apps yet.

any help at all is appreciated, Thanks,
Locky

You need to use

puts Flt.DecNum(22.1138)

or add

include Flt

after the require

cheers

require ‘flt’
puts Flt.DecNum(22.1138)

require ‘flt’
include Flt #or ‘Flt’
puts Flt.DecNum(22.1138) #or DecNum(22.1138)

all give the same error.

i’m still banging my head on this one, and will post more info as I dig
it up.

thanks again.

/facepalm

thanks Marvin, i got my arguments crossed and though -T was for more
verbose debugging output. after removing it, the gem works properly!

thanks also to Chris, adding the Include statement fixed the script,
without that it was returning “test.rb:5:in <main>': undefined methodDecNum’ for main:Object (NoMethodError)”

i’m a bit confused as to why “include” is needed after requiring a gem,
as none of the guides or examples i’ve run across mention anything but
the require statement.

~L

Am Tue, 10 Jul 2012 01:13:44 +0900
schrieb locky thindromen [email protected]:

ruby -T test.rb
/usr/lib/ruby/1.9.1/rubygems/specification.rb:268:in `[]': Insecure
operation - [] (SecurityError)

Why do you use the -T option? This sets Ruby’s $SAFE level, which
certainly isn’t what you want as a newby. It tells Ruby to be
super-eager on certain operations, and it seems your gem is bitten by
that. Do not use -T, and it should work.

Vale,
Marvin

Am Tue, 10 Jul 2012 02:21:37 +0900
schrieb locky thindromen [email protected]:

/facepalm

thanks Marvin, i got my arguments crossed and though -T was for more
verbose debugging output. after removing it, the gem works properly!

You’re welcome :slight_smile:
For the more verbose debugging output, you should check out the -W and
-d options (the latter giving you a mess of output in most cases).

Vale,
Marvin

locky thindromen wrote in post #1068024:

i’m a bit confused as to why “include” is needed after requiring a gem,
as none of the guides or examples i’ve run across mention anything but
the require statement.

You need the “include” if you want to call methods without specifying
the Module they’re in.

Since DecNum is a method of Flt, you’d normally have to call the method
by writing

Flt.DecNum(…)

But instead you can include the module in the root object, which allows
you to omit the “Flt”:

include all methods and constants of Flt in the root object

include Flt

call DecNum:

DecNum(…)