Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Arary : count duplicated object

2 views
Skip to first unread message

Josselin

unread,
Jun 29, 2006, 5:41:52 AM6/29/06
to
I look into an array booked_days to see if an object d (which is a
date object) is included using :

if @booked_days.include?(d)

result is true/false, fine...
but is there any way to know if the object (d) is duplicated in the array ?

something like

if @booked_days.duplicated?(d) (getting true/false)
or
if @booked_days.count?(d) (getting the number of duplicated objects)

joss

Robert Klemme

unread,
Jun 29, 2006, 5:45:29 AM6/29/06
to

@booked_days.inject(0) {|cnt, el| d == el ? cnt.succ : cnt}
@booked_days.select {|el| d == el}.size

HTH

Kind regards

robert

Josselin

unread,
Jun 29, 2006, 11:24:44 AM6/29/06
to

thanks a lot... Robert !
As all newrubies, I'll spend the night to translate it (in order to
ameliorate my rating...)
my interest for Ruby is growing everytime I see how powerful it can be !!

Robert Klemme

unread,
Jul 2, 2006, 5:13:29 AM7/2/06
to
Josselin <joss...@wanadoo.fr> wrote:
> thanks a lot... Robert !
> As all newrubies, I'll spend the night to translate it (in order to
> ameliorate my rating...)

Did you succeed or rather want some comments in the code?

> my interest for Ruby is growing everytime I see how powerful it can
> be !!

:-)

PS: Here's an even more general solution that counts occurrences of *all*
elements in an Enumerable:

counts = enum.inject(Hash.new(0)) {|cnt,e| cnt[e]+=1; cnt}

Kind regards

robert

Josselin

unread,
Jul 3, 2006, 1:53:10 AM7/3/06
to

running fine, but had no time actually to look into the code (soccer
world cup, France-Brasil !)

Robert Klemme

unread,
Jul 3, 2006, 3:26:31 AM7/3/06
to
Josselin wrote:
> running fine, but had no time actually to look into the code (soccer
> world cup, France-Brasil !)

:-) Congrats! Now there's only old and new Europe left in the cup...
At the moment I consider the French to be favorite but I keep my fingers
crossed for the Germans of course.

Kind regards

robert

Josselin

unread,
Jul 3, 2006, 5:50:47 AM7/3/06
to

Even if the French team is more 'experienced' I still consider Germany
has the favorite beacuse playing home (as the French in 98..) and more
I'd like the Germans get the same feeling we got in 98... they deserve
it ! I consider this cup as beautiful as 98 because of the Germans
people behavior... Germany-France will be my favorite final !!!

as you have a lot of Ruby knowledge , I would like to 'exchange'
dot-commas in a string

s= "123.456,78" -> "123,456.78"
I now I should use a s.gsub with a block, but I don't know actually
how to write it... (I can write 3 gsubs.... but it would be ugly.....)

joss

Robert Klemme

unread,
Jul 3, 2006, 5:58:26 AM7/3/06
to

>> "123.456,78".gsub(/[.,]/) {|m| m == "." ? "," : "."}
=> "123,456.78"

But his is far better:

>> "123.456,78".tr ".,", ",."
=> "123,456.78"

Cheers

robert

0 new messages