Forum: IronRuby undefined Method 'downcase'

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
83e58c9f13b3df3a68adfe5263e85725?d=identicon&s=25 Marco Kotrotsos (kotrotsos)
on 2009-03-18 23:10

Hi and goodevening, hope someone can help

i am trying to get some sort of scripting support going. I have the
following code, which executes a ruby method and return the result.
However, it is returning a method not found error from IronRuby itself

some code ommited but it's based on the standard example. (I did make
reference to all the dlls IronRuby and IronRuby.Libraries)

var engine = IronRuby.Ruby.CreateEngine();
returnvalue = engine.Operations.InvokeMember(instance, method,
arg).ToString();



I am running the following ruby code as a test...

class Plotlight
def get_message(a)
res = "Hello- from Ruby " << a
res
end

def swapcase(a)
res = a.downcase
res
end
end

Now, when running the method get_message("something") things work great
however when running something that has a reference to the standard
library (swapcase in this example) it will return the error

$exception {"undefined method `downcase' for fooBAR:ClrString"}
System.Exception {System.MissingMethodException}

running the code through ir.exe works without any problems

Do I need to make a call/reference to the library? and if I do, how do I
do that?

Hope someone can help! Thank you very much.

Kind regards, Marco
1e39a948a58f4569c53d971c2e120920?d=identicon&s=25 Ivan Porto carrero (casualjim)
on 2009-03-18 23:54
(Received via mailing list)
A CLR string is not the same as a ruby string. Calling .to_s before
downcase
should work or you can monkey patch System::String  and add the method
downcase to the clr string class if it bothers you too much


class System::String

  def downcase
     self.to_s.downcase
  end

end
83e58c9f13b3df3a68adfe5263e85725?d=identicon&s=25 Marco Kotrotsos (kotrotsos)
on 2009-03-19 08:08
Thank you Ivan, that has been most helpful!

Marco


Ivan Porto carrero wrote:
> A CLR string is not the same as a ruby string. Calling .to_s before
> downcase
> should work or you can monkey patch System::String  and add the method
> downcase to the clr string class if it bothers you too much
>
>
> class System::String
>
>   def downcase
>      self.to_s.downcase
>   end
>
> end
83e58c9f13b3df3a68adfe5263e85725?d=identicon&s=25 Marco Kotrotsos (kotrotsos)
on 2009-03-19 17:33
i'm sorry, another one popped up and I hope you can help me with it

it's with this example

class Plotlight
  def fib n
    if n<2
      n
    else
     res = fib(n-2)+fib(n-1)
    end
  end
end


when executing, I get Object must be of type String. I suspect this to
be because I am feeding it a String argument. When I do this

class Plotlight
  def fib arg
    n = arg.to_i
    if n<2
      n
    else
     res = fib(n-2)+fib(n-1)
    end
  end
end

it now says

+    $exception  {"undefined method `to_i' for 12:ClrString"}
System.Exception {System.MissingMethodException}


so it again must be because it's not an expected Integer...but how do I
work around this one? I am trying to understand how this works.

Thanks
Marco





Marco Kotrotsos wrote:
> Thank you Ivan, that has been most helpful!
>
> Marco
>
>
> Ivan Porto carrero wrote:
>> A CLR string is not the same as a ruby string. Calling .to_s before
>> downcase
>> should work or you can monkey patch System::String  and add the method
>> downcase to the clr string class if it bothers you too much
>>
>>
>> class System::String
>>
>>   def downcase
>>      self.to_s.downcase
>>   end
>>
>> end
83e58c9f13b3df3a68adfe5263e85725?d=identicon&s=25 Marco Kotrotsos (kotrotsos)
on 2009-03-19 17:40
ugh (slap head)

it was

n = arg.to_s.to_i

so nasty...:)

Marco


Marco Kotrotsos wrote:
> i'm sorry, another one popped up and I hope you can help me with it
>
> it's with this example
>
> class Plotlight
>   def fib n
>     if n<2
>       n
>     else
>      res = fib(n-2)+fib(n-1)
>     end
>   end
> end
>
>
> when executing, I get Object must be of type String. I suspect this to
> be because I am feeding it a String argument. When I do this
>
> class Plotlight
>   def fib arg
>     n = arg.to_i
>     if n<2
>       n
>     else
>      res = fib(n-2)+fib(n-1)
>     end
>   end
> end
>
> it now says
>
> +    $exception  {"undefined method `to_i' for 12:ClrString"}
> System.Exception {System.MissingMethodException}
>
>
> so it again must be because it's not an expected Integer...but how do I
> work around this one? I am trying to understand how this works.
>
> Thanks
> Marco
>
>
>
F983f0c990cba2fe743ef62a975ec99c?d=identicon&s=25 Curt Hagenlocher (Guest)
on 2009-03-19 18:14
(Received via mailing list)
Out of curiosity, why are you passing a string to this method through
the hosting interface instead of just passing an int?  What does the
calling code look like?
83e58c9f13b3df3a68adfe5263e85725?d=identicon&s=25 Marco Kotrotsos (kotrotsos)
on 2009-03-19 19:20
returnvalue = engine.Operations.InvokeMember(instance, method,
arg).ToString();

whereas arg is always String at the moment. (will change somewhere in
the future)

I can probably do a type check on arg...

Marco





Curt Hagenlocher wrote:
> Out of curiosity, why are you passing a string to this method through
> the hosting interface instead of just passing an int?  What does the
> calling code look like?
This topic is locked and can not be replied to.