Arary : count duplicated object

J

Josselin

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
 
R

Robert Klemme

Josselin said:
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)

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

HTH

Kind regards

robert
 
J

Josselin

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

HTH

Kind regards

robert

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 !!
 
R

Robert Klemme

Josselin said:
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
 
J

Josselin

Josselin said:
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

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

Robert Klemme

Josselin said:
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
 
J

Josselin

:) 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

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
 
R

Robert Klemme

Josselin said:
:) 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

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.....)
"123.456,78".gsub(/[.,]/) {|m| m == "." ? "," : "."}
=> "123,456.78"

But his is far better:
=> "123,456.78"

Cheers

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,206
Messages
2,571,074
Members
47,679
Latest member
Justforamoment

Latest Threads

Top