From: Francisco Laguna de la Vera Date: 2008-01-04T04:22:36+09:00 Subject: Re: same Methode name but different Parameter Aloha! I'm afraid what you wrote there doesn't quite do what you expected. Some pointers on what (I think) happens: class Euro def Euro.new_from_cent_only (cent) @cent = cent end end To understand whats going on, you must know two little things, that go a long way: 1) A method returns the value of the last line of the method. In this case that means the value of "@cent = cent". and 2) An assignment simply has as its value the assigned value (so you can write "a = b = c = 3"), which means the method simply returns its argument. Euro.new_fron_cent_only(10) will return the Fixnum instance for 10. The method also doesn't create an instance of your class. You need an #initialize method for that. Another way you might want to go about this: class Euro def initialize(cent) @cent = cent end def Euro.new_from_cent_only(cent) Euro.new(cent) end def Euro.new_from_euro_cent(euro, cent) Euro.new((euro*100) + cent) end end Though this certainly feels a bit clumsy. Better go for the afore mentioned trick with the *args. So if you want something weirder you could add a euros method to Float class Float def euros Euro.new((self*100).to_i) end end So you can write "42.50.euros" Gentle Breezes Cisco Am 03.01.2008 um 19:52 schrieb Salai Khine: > Dear all, > > Thankyou very much. We have finally solve problem like below. I > think it is > not the best way. > > > class Euro > > def Euro.new_from_cent_only (cent) > @cent = cent > end > > def Euro.new_from_euro_cent (euro,cent) > @cent = (euro * 100) + cent > end > > def Euro.new_from_internal_value (an_internal_value) > Euro.new_from_cent_only(an_internal_value) > end > end > > test1 = Euro.new_from_cent_only(10) > test2 = Euro.new_from_euro_cent(11,20) > test3 = Euro.new_from_internal_value(200) > test4 = Euro.new_from_internal_value(test3) > test5 = Euro.new_from_internal_value(200) > > puts test1 > puts test2 === test3 > puts test3 === test5 <---- true > puts test4.object_id > puts test5.object_id > > > now, we have another problem. How can we test .. that > test3 > test5 > have differents object. ??? > > > > regards, > > salai. > (Ruby Newbie) > > > > On Jan 3, 2008 5:43 PM, Rick DeNatale wrote: > >> On Jan 3, 2008 11:16 AM, Trans wrote: >>> >>> >>> On Jan 3, 10:53 am, "Salai Khine" wrote: >>>> Dear all, >>>> >>>> can i use in Ruby the *same Methode name but different Parameter*?? >>>> >>>> like >>>> >>>> def new (cent) >>>> >>>> end >>>> >>>> def new(euro,cent) >>>> >>>> end. >>> >>> def new(*args) >>> euro, cent = *args >>> ... >> >> Well, this didn't really answer the OPs question, and is a bit >> misleading. >> >> calling this with one argument will set euro to ni, and cent to the >> argument, which probably isn't what's looked for. >> >> Answering more directly, no Ruby methods are named only by the name, >> and there's no notion of overloading with different parameter types. >> >> First an aside, since you named this method new, I'm guessing that >> you >> are talking about creating a new object, this is (probably) another >> difference between Ruby and whatever language you have used before. >> In ruby the new method is almost never overriden (and it's a class >> method anyway so it would be def self.new;end). Instead new objects >> are intialized through an instance method called initialize. >> >> Okay, that said there are various techniques to allow for some >> variation in parameters to a method. >> >> One is the use of a final argument prefixed by * this collects any >> arguments passed left over after any prior arguments are satisfied to >> an array. So let's say you were doing a class representing Money >> valuated in Euros, and that you wanted EuroMoney.new(10) to create an >> object containing 10 Euro cents, and EuroMoney.new(1,50) to represent >> 1 and 1/2 Euros. You could do this with something like >> >> class EuroMoney >> def initialize(*args) >> raise ArgumentError unless (1..2).include?(args.length) >> @cents = args.last >> if args.length > 1 >> @euros = args.first >> else >> @euros = 0 >> end >> end >> end >> >> Now lets' say instead we want to be more flexible and have a money >> class which has both a value and a currency, and you want to >> specify a >> unit amount, and a currency with a default. There are at least two >> ways to do this: >> >> 1) currency argument with default value: >> >> class Money >> def initialize(value, currency = :euros) >> @value, @currency = value, currency >> end >> end >> >> So 1.50 Euros could be created either with: >> >> Money.new(150) >> or >> Money.new(150, :euros) >> >> and the equivalent in US Dollars would be: >> >> Money.new(1500, :"US$") >> >> 2) use a hash argument to get the equivalent of keyword arguments >> >> class Money >> def initialize(&args={}) >> args = {:currency => :euros}.merge args # this >> provides a default for currency >> @currency = args[:currency] >> @value = args[:value] >> end >> end >> >> A formal parameter with an & prefix is a hash. >> >> So we would have >> >> Money.new(:value => 150) >> Money.new(:currency => :euros, :value => 150) >> and >> Money.new(:currency => :"US$", :value => 1500) >> >> as the equivalents to the previous examples. >> >> The second approach is probably overkill, on the other hand, with >> some >> more coding, one could use it to model more general concepts of money >> with more code in the initialize method and support things like: >> >> Money.new(:currency => :old_imperial_english, :shillings => >> 10, :pounds >> => 5) >> >> which would compute the unit value based on the components in a >> flexible >> manner. >> >> -- >> Rick DeNatale >> >> My blog on Ruby >> http://talklikeaduck.denhaven2.com/ >> >>