From: "Spax T." Date: 2011-09-01T08:32:35+09:00 Subject: Re: how to pass a variable to a method in an existing class? --Marcin's way is perfect; but there is something we don't understand about your requirement: "...I want to use the string as a variable def convert(x) > #code to convert the x ..." then..."in the irb I can use the new method in the way as: > > "a".convert <---what's happened to ur variable x??? Best Regards, frank hi wrote in post #1019491: > Hi, > > I am a new ruby user and I want to add a function to an existing class. > I googled the answer and found part of the solution. All findings on the > web adding a new method without a variable. In my case I want to use the > string as a variable. Here is what I want to do: > class String > > def convert(x) > #code to convert the x > x.upcase > end > end > > What I want is in the irb I can use the new method in the way as: > > "a".convert > > This will convert a to upper case. However, it does not take the "a" as > the input variable and generating error > ArgumentError: wrong number of arguments (0 for 1) > > How do I acheive what I want? > > Thanks > > Frank -- Posted via http://www.ruby-forum.com/.