# Mscorlib:0:in \`ThrowArgumentException' error

**URL:** <https://www.ruby-forum.com/t/mscorlibin-throwargumentexception-error/193018>\
**Category:** IronRuby\
**Created:** [August 10, 2010, 3:16am UTC](https://www.ruby-forum.com/t/mscorlibin-throwargumentexception-error/193018 "2010-08-10T03:16:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![michbukana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@michbukana](https://www.ruby-forum.com/u/michbukana)\
**Post date:** [August 10, 2010, 3:16am UTC](https://www.ruby-forum.com/t/mscorlibin-throwargumentexception-error/193018/1 "2010-08-10T03:16:05Z")

</div>

Hi all,

I’m having an error that I can’t figure out what it is:

## This portion of code works perfectly by itself

## config = [{:QuantityPer =\> “#{2+2}”}] config.each { |xx| if xx[:QuantityPer].include? ‘#{’ then xxx = xx[:QuantityPer].to\_s xxx = xxx[2…(xxx.size-2)] puts xxx xx[:QuantityPer] = eval(xxx) puts xx[:QuantityPer] end }

## prints 2+2 4 as supposed

## However in the middle of a routine throws this error:

## 2+2 mscorlib:0:in `ThrowArgumentException': An item with the same key has already been added. (ArgumentError) from mscorlib:0:in`Insert’ from ./xxx\_master.rb:641:in `eval’

the line 641 actually is the one corresponding to the beginning of the  
“config.each” statement and not the one for the eval…

Could someone point me in the right direction?

Thank you in advance

Eduardo blumenfeld

---

<div class="post-metadata">

**Author:** ![michbukana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@michbukana](https://www.ruby-forum.com/u/michbukana)\
**Post date:** [August 28, 2010, 4:33pm UTC](https://www.ruby-forum.com/t/mscorlibin-throwargumentexception-error/193018/2 "2010-08-28T16:33:34Z")

</div>

Any Ideas?

Thank you in advance,

Eduardo B.

---

<div class="post-metadata">

**Author:** ![michbukana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@michbukana](https://www.ruby-forum.com/u/michbukana)\
**Post date:** [August 29, 2010, 12:07pm UTC](https://www.ruby-forum.com/t/mscorlibin-throwargumentexception-error/193018/3 "2010-08-29T12:07:57Z")

</div>

Hi Eduardo,

What is config’s type? Is it a regular Ruby list, or is it some sort of  
collection returned from a .NET library? Maybe it’s one of the items in  
the  
config - could you provide a listing of the items in the config when you  
run  
the code? It would also help if I could some of the surrounding routine.

I do find it hard to believe that’s exactly what your code looks like,  
unless you actually have a list of hashes where every key is  
:QuantityPer.  
As an example, the following breaks:

config = [{:QuantityPer =\> “#{2+2}”}, {:foo =\> “bar”}]

# … the rest of your code here …

# Prints the following:

2+2  
4  
NoMethodError: undefined method `include?' for nil:NilClass from (irb):28 from (irb):28:in`each’  
from (irb):28  
from C:/IronRuby/lib/ruby/1.8/irb.rb:150:in `eval_input' from C:/IronRuby/lib/ruby/1.8/irb.rb:257:in`signal\_status’  
from C:/IronRuby/lib/ruby/1.8/irb.rb:147:in `eval_input' from C:/IronRuby/lib/ruby/1.8/irb.rb:146:in`eval\_input’  
from C:/IronRuby/lib/ruby/1.8/irb.rb:70:in `start' from C:/IronRuby/lib/ruby/1.8/irb.rb:69:in`catch’  
from C:/IronRuby/lib/ruby/1.8/irb.rb:69:in `start’  
from C:/IronRuby/bin/irb:13

Regards,

Charles

---

<div class="post-metadata">

**Author:** ![michbukana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@michbukana](https://www.ruby-forum.com/u/michbukana)\
**Post date:** [August 29, 2010, 4:06pm UTC](https://www.ruby-forum.com/t/mscorlibin-throwargumentexception-error/193018/4 "2010-08-29T16:06:55Z")

</div>

Thank you Charles for the answer,

config is an active record produced list, an array of rows brought from  
a sql table that has one of its attributes (table column) =\>  
:QuantityPer

## This is the portion of the code I’m using

config = Term.find\_by\_sql(“sp\_someStoredProc @someparameter=#{param1}”)  
config.each { |xx| if xx[:QuantityPer].nil? then nil else  
if xx[:QuantityPer].include? ‘#{’ then  
xxx = xx[:QuantityPer].to\_s  
xxx = xxx[2…(xxx.size-2)]  
puts xxx

## error line

xx[:QuantityPer] = eval(xxx)

## error line

## puts xx[:QuantityPer] end end }

The error line line is the one I’m showing  
EUREKA!!!

While I was answering you, I think figured out the problem, (I have to  
test it though)

## The problem has to do with the fact that the active record model has attr\_readonly set as:

## class Term \< ActiveRecord::Base attr\_readonly end

What is puzzling to me is the cryptic error, not having a complete trace  
of  
the troubling gem (in this case the active\_record) with this kind of  
errors

Thank you, Thank you

I will post the results of the test as soon as It works!

Eduardo B.

---

<div class="post-metadata">

**Author:** ![michbukana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@michbukana](https://www.ruby-forum.com/u/michbukana)\
**Post date:** [September 4, 2010, 7:08am UTC](https://www.ruby-forum.com/t/mscorlibin-throwargumentexception-error/193018/5 "2010-09-04T07:08:28Z")

</div>

Hi all,

Unfortunately it didn’t work,

I believe that there is some kind of problem in mscorlib  
when you try to do an eval inside a loop with active record objects.

Eduardo B. wrote:

> Thank you Charles for the answer,
> 
> config is an active record produced list, an array of rows brought from  
> a sql table that has one of its attributes (table column) =\>  
> :QuantityPer
> 
> ## This is the portion of the code I’m using
> 
> config = Term.find\_by\_sql(“sp\_someStoredProc @someparameter=#{param1}”)  
> config.each { |xx| if xx[:QuantityPer].nil? then nil else  
> if xx[:QuantityPer].include? ‘#{’ then  
> xxx = xx[:QuantityPer].to\_s  
> xxx = xxx[2..(xxx.size-2)]  
> puts xxx

# =\> “some\_proc(argument,argument2)”

## I see in the console the xxx variable… if at this point I put this code:

## puts “#{xxx}”

it just prints (again) the xxx variable without  
evaluating it at all

## if at this point I put this code instead:

## aaa = eval(xxx)

then I get this error:

mscorlib:0:in `ThrowArgumentException': An item with the same key has already been added. (ArgumentError) from mscorlib:0:in `Insert’

Any pointers?

Is that an mscorlib bug?

---

<div class="post-metadata">

**Author:** ![michbukana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@michbukana](https://www.ruby-forum.com/u/michbukana)\
**Post date:** [September 9, 2010, 10:02pm UTC](https://www.ruby-forum.com/t/mscorlibin-throwargumentexception-error/193018/6 "2010-09-09T22:02:36Z")

</div>

Hi Eduardo,

This looks like it could be a bug in IronRuby… or possibly the  
ActiveRecord adapter. That exception that is being thrown is from a  
Hashtable or Dictionary; a value is being inserted with an existing key.  
Typical Ruby code won’t ever use a .NET specific type such as Hashtable  
or  
Dictionary (of course), which makes me think that this might be a bug in  
IronRuby… or possibly your ActiveRecord adapter (assuming it’s tailor  
made  
for IronRuby).

I’d hate to make you jump through hoops, but it would be great if you  
could  
lend me a hand in figuring this one out. If you could send me a small  
repro, that would be great; baring that, I have some questions for you:

What sort of AR adapter are you using?

Could you give me the “actual” stack trace for this exception? This  
should  
do the trick:

begin

# … the line of code that throws …

rescue Exception =\> e

# use the following to get the actual .NET/System.Exception stack

trace:  
puts e.StackTrace  
end

Thanks,

Charles

---

<div class="post-metadata">

**Author:** ![michbukana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@michbukana](https://www.ruby-forum.com/u/michbukana)\
**Post date:** [September 6, 2010, 12:59am UTC](https://www.ruby-forum.com/t/mscorlibin-throwargumentexception-error/193018/7 "2010-09-06T00:59:54Z")

</div>

Hi all,

I finally solved the issue:  
By calling another procedure and within that procedure do the eval, it  
works perfectly.

However, this seems to be a bug anyways…

Regards,

Eduardo
