Something messy

body { font-family: “Calibri”,“Slate
Pro”,“sans-serif”; color:#262626 }
‎Hello. 

Half-newbie in
Ruby, I tried to write a few lines of code.
I would like to
show every french county following an array. 
Problem is
: for Corsica I get 2A (corse du sud) and 2B (haute corse) then i shall
go back to 21.
Here is my
code:

What’s wrong about it ? As an outpout
everything works fine till 21. I get twice
haute-corse. 

Thanks for help. I know my
code is messy and I sometimes have problems with
loops.


dep
=["",“Ain”,“Aisne”,“Allier”,“Hautes-Alpes”,“Alpes-de-Haute-Provence”,“Alpes-Maritimes”,“Ardèche”,“Ardennes”,“Ariège”,“Aube”,“Aude”,“Aveyron”,
“Bouches-du-Rhône”,“Calvados”,“Cantal”,“Charente”,“Charente-Maritime”,“Cher”,“Corrèze”,“Corse-du-sud”,“Haute-corse”,“Côte-d’or”,
“Côtes-d’armor”,“Creuse”,“Dordogne”,“Doubs”,“Drôme”,“Eure”,“Eure-et-Loir”,“Finistère”,“Gard”,“Haute-Garonne”,“Gers”,“Gironde”,“Hérault”,
“Ile-et-Vilaine”,“Indre”,“Indre-et-Loire”,“Isère”,“Jura”,“Landes”,“Loir-et-Cher”,“Loire”,“Haute-Loire”,“Loire-Atlantique”,“Loiret”,“Lot”,
“Lot-et-Garonne”,“Lozère”,“Maine-et-Loire”,“Manche”,“Marne”,“Haute-Marne”,“Mayenne”,“Meurthe-et-Moselle”,“Meuse”,“Morbihan”,
“Moselle”,“Nièvre”,“Nord”,“Oise”,“Orne”,“Pas-de-Calais”,“Puy-de-Dôme”,“Pyrénées-Atlantiques”,“Hautes-Pyrénées”,“Pyrénées-Orientales”,
“Bas-Rhin”,“Haut-Rhin”,“Rhône”,“Haute-Saône”,“Saône-et-Loire”,“Sarthe”,“Savoie”,“Haute-Savoie”,“Paris”,“Seine-Maritime”,“Seine-et-Marne”,
“Yvelines”,“Deux-Sèvres”,“Somme”,“Tarn”,“Tarn-et-Garonne”,“Var”,“Vaucluse”,“Vendée”,“Vienne”,“Haute-Vienne”,“Vosges”,“Yonne”,
“Territoire
de
Belfort”,“Essonne”,“Hauts-de-Seine”,“Seine-Saint-Denis”,“Val-de-Marne”,“Val-d’oise”,“Mayotte”,“Guadeloupe”,“Guyane”,“Martinique”,“Réunion”]



i

-1


system(“cls”)

dep.each
do |depart|
i+=1
if i ==
0

print “Num    |     Département    
  |  Préfecture  \n”
print
“------------------------------------------------\n”

end
sleep 0.4
if i ==
20

print “2A     |  #{depart}\n”

end
if i == 21
print “2B
    |  #{depart}\n”
i =
20

i += 1


end

if i < 10 && i != 0
&& i != 20
   print “0#{i}  
  |  #{depart}\n”
elsif i > 9 && i != 0
&& i != 20 
print “#{i}     |
 #{depart}\n”

end
if i >
95

i = 970

end
end

system
(“pause”)
s = gets.to_i
if s > 9
print
"#{dep[s]} (#{s.to_s})\n"
else
print "#{dep[s]}
(0#{s.to_s})\n"
end
system
(“pause”) 


Envoyé de
mon smartphone BlackBerry 10 sur le réseau SFR.

After printing ‘Haute-corse’ the first time, i is equal to 21 and the
following code will print ‘Haute-corse’ a second time.

elsif i > 9 && i != 0 && i != 20
print “#{i} | #{depart}\n”

One solution is to set a flag to indicate that you have processed
Corsica so that when i is reset to 21 it will not be picked up twice in
your loop. Overall, this code is extremely cumbersome and lacking in
best practices.

i = -1

system(“cls”)
corsica_done = false
dep.each do |depart|
i+=1
if i == 0
print “Num | Département | Préfecture \n”
print “------------------------------------------------\n”
end
sleep 0.4
unless corsica_done
if i == 20
print “2A | #{depart}\n”
end
if i == 21
print “2B | #{depart}\n”
i = 20
corsica_done = true
end
end

if i < 10 && i != 0 && i != 20
print “0#{i} | #{depart}\n”
elsif i > 9 && i != 0 && i != 20
print “#{i} | #{depart}\n”
end
if i > 95
i = 970

end
end

system (“pause”)
s = gets.to_i
if s > 9
print “#{dep[s]} (#{s.to_s})\n”
else
print “#{dep[s]} (0#{s.to_s})\n”
end
system (“pause”)

Arthur Lyman
(914) 693-5113
mailto://[email protected]

On Thursday, September 11, 2014 9:02 AM, “[email protected]
[email protected] wrote:

‎Hello.

Half-newbie in Ruby, I tried to write a few lines of code.
I would like to show every french county following an array.
Problem is : for Corsica I get 2A (corse du sud) and 2B (haute corse)
then i shall go back to 21.
Here is my code:

What’s wrong about it ? As an outpout everything works fine till 21. I
get twice haute-corse.

Thanks for help. I know my code is messy and I sometimes have problems
with loops.

dep
=[“”,“Ain”,“Aisne”,“Allier”,“Hautes-Alpes”,“Alpes-de-Haute-Provence”,“Alpes-Maritimes”,“Ardèche”,“Ardennes”,“Ariège”,“Aube”,“Aude”,“Aveyron”,
“Bouches-du-Rhône”,“Calvados”,“Cantal”,“Charente”,“Charente-Maritime”,“Cher”,“Corrèze”,“Corse-du-sud”,“Haute-corse”,“Côte-d’or”,
“Côtes-d’armor”,“Creuse”,“Dordogne”,“Doubs”,“Drôme”,“Eure”,“Eure-et-Loir”,“Finistère”,“Gard”,“Haute-Garonne”,“Gers”,“Gironde”,“Hérault”,
“Ile-et-Vilaine”,“Indre”,“Indre-et-Loire”,“Isère”,“Jura”,“Landes”,“Loir-et-Cher”,“Loire”,“Haute-Loire”,“Loire-Atlantique”,“Loiret”,“Lot”,
“Lot-et-Garonne”,“Lozère”,“Maine-et-Loire”,“Manche”,“Marne”,“Haute-Marne”,“Mayenne”,“Meurthe-et-Moselle”,“Meuse”,“Morbihan”,
“Moselle”,“Nièvre”,“Nord”,“Oise”,“Orne”,“Pas-de-Calais”,“Puy-de-Dôme”,“Pyrénées-Atlantiques”,“Hautes-Pyrénées”,“Pyrénées-Orientales”,
“Bas-Rhin”,“Haut-Rhin”,“Rhône”,“Haute-Saône”,“Saône-et-Loire”,“Sarthe”,“Savoie”,“Haute-Savoie”,“Paris”,“Seine-Maritime”,“Seine-et-Marne”,
“Yvelines”,“Deux-Sèvres”,“Somme”,“Tarn”,“Tarn-et-Garonne”,“Var”,“Vaucluse”,“Vendée”,“Vienne”,“Haute-Vienne”,“Vosges”,“Yonne”,
“Territoire de
Belfort”,“Essonne”,“Hauts-de-Seine”,“Seine-Saint-Denis”,“Val-de-Marne”,“Val-d’oise”,“Mayotte”,“Guadeloupe”,“Guyane”,“Martinique”,“Réunion”]

i = -1

system(“cls”)

dep.each do |depart|
i+=1
if i == 0
print “Num | Département | Préfecture \n”
print “------------------------------------------------\n”
end
sleep 0.4
if i == 20
print “2A | #{depart}\n”
end
if i == 21
print “2B | #{depart}\n”
i = 20
i += 1
end

if i < 10 && i != 0 && i != 20
print “0#{i} | #{depart}\n”
elsif i > 9 && i != 0 && i != 20
print “#{i} | #{depart}\n”

end
if i > 95
i = 970
end
end

system (“pause”)
s = gets.to_i
if s > 9
print “#{dep[s]} (#{s.to_s})\n”
else
print “#{dep[s]} (0#{s.to_s})\n”
end
system (“pause”)

Envoyé de mon smartphone BlackBerry 10 sur le réseau SFR.

On Thu, Sep 11, 2014 at 4:49 PM, Jesús Gabriel y Galán
[email protected] wrote:

“Territoire de
Belfort”,“Essonne”,“Hauts-de-Seine”,“Seine-Saint-Denis”,“Val-de-Marne”,“Val-d’oise”,“Mayotte”,“Guadeloupe”,“Guyane”,“Martinique”,“Réunion”]

system(“cls”)

print “Num | Département | Préfecture \n”
print “------------------------------------------------\n”

Sorry to respond to myself, but as you see below, print with \n at the
end is just what puts does. Also the dashes could be dynamically
calculated based on the previous string length, so if you modify the
header you don’t have to add anything else:

header = "Num | Département | Préfecture "
puts header
puts “-” * header.length

Just another little trick.

Jesus.

Thanks a lot. I’m going to work it out and clean this mess up.

I always get lost when it comes to if statements.

Need to take time to learn more.

Ronan.

Envoy de mon smartphone BlackBerry 10 sur le rseau SFR.
Message d’origine
De: Jess Gabriel y Galn
Envoy: jeudi 11 septembre 2014 16:53
: Ruby users
Rpondre : Ruby users
Objet: Re: Something messy

On Thu, Sep 11, 2014 at 4:49 PM, Jess Gabriel y Galn
[email protected] wrote:

“Territoire de
Belfort”,“Essonne”,“Hauts-de-Seine”,“Seine-Saint-Denis”,“Val-de-Marne”,“Val-d’oise”,“Mayotte”,“Guadeloupe”,“Guyane”,“Martinique”,“Runion”]

system(“cls”)

print “Num | Dpartement | Prfecture \n”
print “------------------------------------------------\n”

Sorry to respond to myself, but as you see below, print with \n at the
end is just what puts does. Also the dashes could be dynamically
calculated based on the previous string length, so if you modify the
header you don’t have to add anything else:

header = "Num | Dpartement | Prfecture "
puts header
puts “-” * header.length

Just another little trick.

Jesus.

2014-09-11 9:53 GMT-07:00 [email protected]:

I always get lost when it comes to if statements.

They can be hard to follow when overused, and there are frequently
more readable approaches. Here’s one example:

dep
=[“Ain”,“Aisne”,“Allier”,“Hautes-Alpes”,“Alpes-de-Haute-Provence”,“Alpes-Maritimes”,“Ardèche”,“Ardennes”,“Ariège”,“Aube”,“Aude”,“Aveyron”,
“Bouches-du-Rhône”,“Calvados”,“Cantal”,“Charente”,“Charente-Maritime”,“Cher”,“Corrèze”,“Corse-du-sud”,“Haute-corse”,“Côte-d’or”,
“Côtes-d’armor”,“Creuse”,“Dordogne”,“Doubs”,“Drôme”,“Eure”,“Eure-et-Loir”,“Finistère”,“Gard”,“Haute-Garonne”,“Gers”,“Gironde”,“Hérault”,
“Ile-et-Vilaine”,“Indre”,“Indre-et-Loire”,“Isère”,“Jura”,“Landes”,“Loir-et-Cher”,“Loire”,“Haute-Loire”,“Loire-Atlantique”,“Loiret”,“Lot”,
“Lot-et-Garonne”,“Lozère”,“Maine-et-Loire”,“Manche”,“Marne”,“Haute-Marne”,“Mayenne”,“Meurthe-et-Moselle”,“Meuse”,“Morbihan”,
“Moselle”,“Nièvre”,“Nord”,“Oise”,“Orne”,“Pas-de-Calais”,“Puy-de-Dôme”,“Pyrénées-Atlantiques”,“Hautes-Pyrénées”,“Pyrénées-Orientales”,
“Bas-Rhin”,“Haut-Rhin”,“Rhône”,“Haute-Saône”,“Saône-et-Loire”,“Sarthe”,“Savoie”,“Haute-Savoie”,“Paris”,“Seine-Maritime”,“Seine-et-Marne”,
“Yvelines”,“Deux-Sèvres”,“Somme”,“Tarn”,“Tarn-et-Garonne”,“Var”,“Vaucluse”,“Vendée”,“Vienne”,“Haute-Vienne”,“Vosges”,“Yonne”,
“Territoire de
Belfort”,“Essonne”,“Hauts-de-Seine”,“Seine-Saint-Denis”,“Val-de-Marne”,“Val-d’oise”,“Mayotte”,“Guadeloupe”,“Guyane”,“Martinique”,“Réunion”]

puts "Num | Département | Préfecture "
puts “------------------------------------------------”

def mark_as(index)
{‘20’ => ‘2A’, ‘21’ => ‘2B’}[“#{index}”] || “%02d” % (index + 1)
end

dep.first(95).each_with_index do |depart, index|
puts sprintf “%s | %s”, mark_as(index), depart
end

Just one possible approach, sans “ifs” :slight_smile:

Good luck,

I shall try this one. I realize to work more on the subject. There are
ways I don’t think of. !

Thanks for the help.

Ronan

Envoyé de mon smartphone BlackBerry 10 sur le réseau SFR.
Message d’origine
De: Hassan S.
Envoyé: jeudi 11 septembre 2014 20:36
À: Ruby users
Répondre à: Ruby users
Objet: Re: Something messy

2014-09-11 9:53 GMT-07:00 [email protected]:

I always get lost when it comes to if statements.

They can be hard to follow when overused, and there are frequently
more readable approaches. Here’s one example:

dep
=[“Ain”,“Aisne”,“Allier”,“Hautes-Alpes”,“Alpes-de-Haute-Provence”,“Alpes-Maritimes”,“Ardèche”,“Ardennes”,“Ariège”,“Aube”,“Aude”,“Aveyron”,
“Bouches-du-Rhône”,“Calvados”,“Cantal”,“Charente”,“Charente-Maritime”,“Cher”,“Corrèze”,“Corse-du-sud”,“Haute-corse”,“Côte-d’or”,
“Côtes-d’armor”,“Creuse”,“Dordogne”,“Doubs”,“Drôme”,“Eure”,“Eure-et-Loir”,“Finistère”,“Gard”,“Haute-Garonne”,“Gers”,“Gironde”,“Hérault”,
“Ile-et-Vilaine”,“Indre”,“Indre-et-Loire”,“Isère”,“Jura”,“Landes”,“Loir-et-Cher”,“Loire”,“Haute-Loire”,“Loire-Atlantique”,“Loiret”,“Lot”,
“Lot-et-Garonne”,“Lozère”,“Maine-et-Loire”,“Manche”,“Marne”,“Haute-Marne”,“Mayenne”,“Meurthe-et-Moselle”,“Meuse”,“Morbihan”,
“Moselle”,“Nièvre”,“Nord”,“Oise”,“Orne”,“Pas-de-Calais”,“Puy-de-Dôme”,“Pyrénées-Atlantiques”,“Hautes-Pyrénées”,“Pyrénées-Orientales”,
“Bas-Rhin”,“Haut-Rhin”,“Rhône”,“Haute-Saône”,“Saône-et-Loire”,“Sarthe”,“Savoie”,“Haute-Savoie”,“Paris”,“Seine-Maritime”,“Seine-et-Marne”,
“Yvelines”,“Deux-Sèvres”,“Somme”,“Tarn”,“Tarn-et-Garonne”,“Var”,“Vaucluse”,“Vendée”,“Vienne”,“Haute-Vienne”,“Vosges”,“Yonne”,
“Territoire de
Belfort”,“Essonne”,“Hauts-de-Seine”,“Seine-Saint-Denis”,“Val-de-Marne”,“Val-d’oise”,“Mayotte”,“Guadeloupe”,“Guyane”,“Martinique”,“Réunion”]

puts "Num | Département | Préfecture "
puts “------------------------------------------------”

def mark_as(index)
{‘20’ => ‘2A’, ‘21’ => ‘2B’}[“#{index}”] || “%02d” % (index + 1)
end

dep.first(95).each_with_index do |depart, index|
puts sprintf “%s | %s”, mark_as(index), depart
end

Just one possible approach, sans “ifs” :slight_smile:

Good luck,

2014-09-11 14:54 GMT+02:00 [email protected]:

“Moselle”,“Nièvre”,“Nord”,“Oise”,“Orne”,“Pas-de-Calais”,“Puy-de-Dôme”,“Pyrénées-Atlantiques”,“Hautes-Pyrénées”,“Pyrénées-Orientales”,

if i == 21
end
print “#{dep[s]} (0#{s.to_s})\n”
end
system (“pause”)

The problem is that these conditions:

if i == 21
print “2B | #{depart}\n”
i = 20
i += 1
end

and

elsif i > 9 && i != 0 && i != 20
print “#{i} | #{depart}\n”
end

are not exclusive for 21. When i is 21, the code will enter in both
conditions, printing twice. Maybe you need that all conditions are
mutually exclusive, being part of the same if/elseif condition.
Something like:

if i == 20

elsif i == 21

elsif i < 10

else # i > 9 except 20 and 21

end

By the way, the structure of your code is pretty messy, you have lots
of ifs, a lot of conditions with numbers, that always smells to me
that it would be good to find some other way to make it cleaner. Also,
you have a lot of unused conditions. For example:

if i < 10 && i != 20 # a number that is less than 10 will never be 20
if i > 0 && i != 0 # a number greater than 9 will never be 0

Some other tips: the condition about < 10 is only for 0 padding. Look
at String#rjust method or the String#% formatting method. The header
could be printed before the loop, removing the condition on 0.

Also, you could use each_with_index, so you don’t have to increment
the i yourself. Btw, your increment of i in the if 21 case is quite
strange, cause you leave it at 21 anyway.

Something like:

dep
=[“”,“Ain”,“Aisne”,“Allier”,“Hautes-Alpes”,“Alpes-de-Haute-Provence”,“Alpes-Maritimes”,“Ardèche”,“Ardennes”,“Ariège”,“Aube”,“Aude”,“Aveyron”,
“Bouches-du-Rhône”,“Calvados”,“Cantal”,“Charente”,“Charente-Maritime”,“Cher”,“Corrèze”,“Corse-du-sud”,“Haute-corse”,“Côte-d’or”,
“Côtes-d’armor”,“Creuse”,“Dordogne”,“Doubs”,“Drôme”,“Eure”,“Eure-et-Loir”,“Finistère”,“Gard”,“Haute-Garonne”,“Gers”,“Gironde”,“Hérault”,
“Ile-et-Vilaine”,“Indre”,“Indre-et-Loire”,“Isère”,“Jura”,“Landes”,“Loir-et-Cher”,“Loire”,“Haute-Loire”,“Loire-Atlantique”,“Loiret”,“Lot”,
“Lot-et-Garonne”,“Lozère”,“Maine-et-Loire”,“Manche”,“Marne”,“Haute-Marne”,“Mayenne”,“Meurthe-et-Moselle”,“Meuse”,“Morbihan”,
“Moselle”,“Nièvre”,“Nord”,“Oise”,“Orne”,“Pas-de-Calais”,“Puy-de-Dôme”,“Pyrénées-Atlantiques”,“Hautes-Pyrénées”,“Pyrénées-Orientales”,
“Bas-Rhin”,“Haut-Rhin”,“Rhône”,“Haute-Saône”,“Saône-et-Loire”,“Sarthe”,“Savoie”,“Haute-Savoie”,“Paris”,“Seine-Maritime”,“Seine-et-Marne”,
“Yvelines”,“Deux-Sèvres”,“Somme”,“Tarn”,“Tarn-et-Garonne”,“Var”,“Vaucluse”,“Vendée”,“Vienne”,“Haute-Vienne”,“Vosges”,“Yonne”,
“Territoire de
Belfort”,“Essonne”,“Hauts-de-Seine”,“Seine-Saint-Denis”,“Val-de-Marne”,“Val-d’oise”,“Mayotte”,“Guadeloupe”,“Guyane”,“Martinique”,“Réunion”]

system(“cls”)

print “Num | Département | Préfecture \n”
print “------------------------------------------------\n”

dep.each_with_index do |depart, i|

num = i + 1
if num == 20
    prefix = "2A"
elsif num == 21
    prefix = "2B"
elsif num > 95
    prefix = "970"
else
    prefix = "%02d" % num
end

puts "#{prefix}     |  #{depart}"

end

Hope this gives you some ideas,

Jesus.

Hello. I tried out your solution. Here’s the code with a comment for I
lost number 21 :wink:

dep
=[“Ain”,“Aisne”,“Allier”,“Hautes-Alpes”,“Alpes-de-Haute-Provence”,“Alpes-Maritimes”,“Ardche”,“Ardennes”,“Arige”,“Aube”,“Aude”,“Aveyron”,
“Bouches-du-Rhne”,“Calvados”,“Cantal”,“Charente”,“Charente-Maritime”,“Cher”,“Corrze”,“Corse-du-sud”,“Haute-corse”,“Cte-d’or”,
“Ctes-d’armor”,“Creuse”,“Dordogne”,“Doubs”,“Drme”,“Eure”,“Eure-et-Loir”,“Finistre”,“Gard”,“Haute-Garonne”,“Gers”,“Gironde”,“Hrault”,
“Ile-et-Vilaine”,“Indre”,“Indre-et-Loire”,“Isre”,“Jura”,“Landes”,“Loir-et-Cher”,“Loire”,“Haute-Loire”,“Loire-Atlantique”,“Loiret”,“Lot”,
“Lot-et-Garonne”,“Lozre”,“Maine-et-Loire”,“Manche”,“Marne”,“Haute-Marne”,“Mayenne”,“Meurthe-et-Moselle”,“Meuse”,“Morbihan”,
“Moselle”,“Nivre”,“Nord”,“Oise”,“Orne”,“Pas-de-Calais”,“Puy-de-Dme”,“Pyrnes-Atlantiques”,“Hautes-Pyrnes”,“Pyrnes-Orientales”,
“Bas-Rhin”,“Haut-Rhin”,“Rhne”,“Haute-Sane”,“Sane-et-Loire”,“Sarthe”,“Savoie”,“Haute-Savoie”,“Paris”,“Seine-Maritime”,“Seine-et-Marne”,
“Yvelines”,“Deux-Svres”,“Somme”,“Tarn”,“Tarn-et-Garonne”,“Var”,“Vaucluse”,“Vende”,“Vienne”,“Haute-Vienne”,“Vosges”,“Yonne”,
“Territoire de
Belfort”,“Essonne”,“Hauts-de-Seine”,“Seine-Saint-Denis”,“Val-de-Marne”,“Val-d’oise”,“Mayotte”,“Guadeloupe”,“Guyane”,“Martinique”,“Runion”]

system(“cls”)

puts “Num | Dpartement | Prfecture \n”
puts “------------------------------------------------\n”

def mark_as(index)
{‘19’=>‘2A’,‘20’=>‘2B’}[“#{index}”] || “%02d” % (index+1)
end

############### COMMENT ###########################################

Here it works fine but I have as an outpout :

19 Corrze (great)

2A Corse du sud (great)

2B Haute corse (great)

22 Cte d’or (Here it shall be number 21 and not 22)

I’m puzzled. Otherwise it works ok.

###################################################################

dep.first(95).each_with_index do |depart, index|
sleep 0.5
puts sprintf"%s | %s",mark_as(index),depart
end

system (“pause”)
s = gets.to_i
if s > 9
print “#{dep[s]} (#{s.to_s})\n”
else
print “#{dep[s]} (0#{s.to_s})\n”
end
system (“pause”)

Envoy de mon smartphone BlackBerry 10 sur le rseau SFR.
Message d’origine
De: Hassan S.
Envoy: jeudi 11 septembre 2014 20:36
: Ruby users
Rpondre : Ruby users
Objet: Re: Something messy

2014-09-11 9:53 GMT-07:00 [email protected]:

I always get lost when it comes to if statements.

They can be hard to follow when overused, and there are frequently
more readable approaches. Here’s one example:

dep
=[“Ain”,“Aisne”,“Allier”,“Hautes-Alpes”,“Alpes-de-Haute-Provence”,“Alpes-Maritimes”,“Ardche”,“Ardennes”,“Arige”,“Aube”,“Aude”,“Aveyron”,
“Bouches-du-Rhne”,“Calvados”,“Cantal”,“Charente”,“Charente-Maritime”,“Cher”,“Corrze”,“Corse-du-sud”,“Haute-corse”,“Cte-d’or”,
“Ctes-d’armor”,“Creuse”,“Dordogne”,“Doubs”,“Drme”,“Eure”,“Eure-et-Loir”,“Finistre”,“Gard”,“Haute-Garonne”,“Gers”,“Gironde”,“Hrault”,
“Ile-et-Vilaine”,“Indre”,“Indre-et-Loire”,“Isre”,“Jura”,“Landes”,“Loir-et-Cher”,“Loire”,“Haute-Loire”,“Loire-Atlantique”,“Loiret”,“Lot”,
“Lot-et-Garonne”,“Lozre”,“Maine-et-Loire”,“Manche”,“Marne”,“Haute-Marne”,“Mayenne”,“Meurthe-et-Moselle”,“Meuse”,“Morbihan”,
“Moselle”,“Nivre”,“Nord”,“Oise”,“Orne”,“Pas-de-Calais”,“Puy-de-Dme”,“Pyrnes-Atlantiques”,“Hautes-Pyrnes”,“Pyrnes-Orientales”,
“Bas-Rhin”,“Haut-Rhin”,“Rhne”,“Haute-Sane”,“Sane-et-Loire”,“Sarthe”,“Savoie”,“Haute-Savoie”,“Paris”,“Seine-Maritime”,“Seine-et-Marne”,
“Yvelines”,“Deux-Svres”,“Somme”,“Tarn”,“Tarn-et-Garonne”,“Var”,“Vaucluse”,“Vende”,“Vienne”,“Haute-Vienne”,“Vosges”,“Yonne”,
“Territoire de
Belfort”,“Essonne”,“Hauts-de-Seine”,“Seine-Saint-Denis”,“Val-de-Marne”,“Val-d’oise”,“Mayotte”,“Guadeloupe”,“Guyane”,“Martinique”,“Runion”]

puts "Num | Dpartement | Prfecture "
puts “------------------------------------------------”

def mark_as(index)
{‘20’ => ‘2A’, ‘21’ => ‘2B’}[“#{index}”] || “%02d” % (index + 1)
end

dep.first(95).each_with_index do |depart, index|
puts sprintf “%s | %s”, mark_as(index), depart
end

Just one possible approach, sans “ifs” :slight_smile:

Good luck,

Thanks for your answer. I’ll try this.

Envoy de mon smartphone BlackBerry 10 sur le rseau SFR.
Message d’origine
De: Jess Gabriel y Galn
Envoy: vendredi 12 septembre 2014 10:48
: Ruby users
Rpondre : Ruby users
Objet: Re: Something messy

2014-09-12 9:52 GMT+02:00 [email protected]:

“Territoire de
Belfort”,“Essonne”,“Hauts-de-Seine”,“Seine-Saint-Denis”,“Val-de-Marne”,“Val-d’oise”,“Mayotte”,“Guadeloupe”,“Guyane”,“Martinique”,“Runion”]
def mark_as(index)

I’m puzzled. Otherwise it works ok.

###################################################################

dep.first(95).each_with_index do |depart, index|
sleep 0.5
puts sprintf"%s | %s",mark_as(index),depart
end

So from 0 to 18, the number should be +1, 19 and 20 are special, and
starting from 21 the number has to be that same one:

def mark_as(index)

btw, hash keys don’t need to be strings

{19 => ‘2A’, 20 => ‘2B’}[index] || “%02d” % (index < 19 ? index + 1 :
index)
end

dep.first(95).each_with_index do |depart, index|
puts sprintf"%s | %s",mark_as(index),depart
end

Now, if you want to account also for the ones bigger than 95, things
start to get messy again. Either you can continue with the hash
approach for those last ones:

{19 => ‘2A’, 20 => ‘2B’, 95 => ‘970’, 96 => ‘970’, …}

or add another condition to the “default” formatting, which then
starts not be so terse anymore:

{19 => ‘2A’, 20 => ‘2B’}[index] || “%02d” % (index < 19 ? index + 1 :
(index > 94 ? ‘970’ : index))

Jesus.

2014-09-12 10:59 GMT+02:00 [email protected]:

Thanks for your answer. I’ll try this.

{19 => ‘2A’, 20 => ‘2B’}[index] || “%02d” % (index < 19 ? index + 1 :
(index > 94 ? ‘970’ : index))

You can also use case:

def mark_as(index)
case index
when 0…18
“%02d” % (index + 1)
when 19
‘2A’
when 20
‘2B’
when 21…94
“%02d” % index
else
‘970’
end
end

Jesus.

2014-09-12 9:52 GMT+02:00 [email protected]:

“Territoire de
Belfort”,“Essonne”,“Hauts-de-Seine”,“Seine-Saint-Denis”,“Val-de-Marne”,“Val-d’oise”,“Mayotte”,“Guadeloupe”,“Guyane”,“Martinique”,“Réunion”]
def mark_as(index)

I’m puzzled. Otherwise it works ok.

###################################################################

dep.first(95).each_with_index do |depart, index|
sleep 0.5
puts sprintf"%s | %s",mark_as(index),depart
end

So from 0 to 18, the number should be +1, 19 and 20 are special, and
starting from 21 the number has to be that same one:

def mark_as(index)
# btw, hash keys don’t need to be strings
{19 => ‘2A’, 20 => ‘2B’}[index] || “%02d” % (index < 19 ? index + 1
: index)
end

dep.first(95).each_with_index do |depart, index|
puts sprintf"%s | %s",mark_as(index),depart
end

Now, if you want to account also for the ones bigger than 95, things
start to get messy again. Either you can continue with the hash
approach for those last ones:

{19 => ‘2A’, 20 => ‘2B’, 95 => ‘970’, 96 => ‘970’, …}

or add another condition to the “default” formatting, which then
starts not be so terse anymore:

{19 => ‘2A’, 20 => ‘2B’}[index] || “%02d” % (index < 19 ? index + 1 :
(index > 94 ? ‘970’ : index))

Jesus.