------ art_44654_23141592.1199386317160
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
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 ent
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 uro.new_from_cent_only(10)
test2 uro.new_from_euro_cent(11,20)
test3 uro.new_from_internal_value(200)
test4 uro.new_from_internal_value(test3)
test5 uro.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 <rick.denatale / gmail.com> wrote:
> On Jan 3, 2008 11:16 AM, Trans <transfire / gmail.com> wrote:
> >
> >
> > On Jan 3, 10:53 am, "Salai Khine" <saya... / gmail.com> 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 rgs.last
> if args.length > 1
> @euros rgs.first
> else
> @euros
> 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 alue, currency
> end
> end
>
> So 1.50 Euros could be created either with:
>
> Money.new(150)
> or
> Money.new(150, :euros)
>
> and the equivalent <G> 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 rgs[:currency]
> @value rgs[: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
>