Helpful Information
 
 
Category: Ruby Programming
Ruby help, programming noob

I'm trying to make a program to automatically make a work schedule.

There's a number of different things I need to implement, such as off days, specific jobs for different people, "on-call" workers, etc.

I tried it one way yesterday but that didn't work well at all. I think the way I was trying to assign people to jobs was too complicated so I started over today.

I decided on setting up a class "Soldier" that will include the persons name as a string, and then have a series of priority codes to decide to which of the different available seats at work they will be assigned. There are three possible values for the person's priority on a job: 2, it's the person's main job; 1, qualifed; or 0, unqualified.

I will put those soldiers in an array called 'team'. I eventually want to write this array to a file at the end of the program, and then import that file the next time it is run, but I'll figure that out later.

Now I was planning to write a series of statements for each job and have it search my array and look for the person who had the highest priority code on that job, then assign that persons name to that job (represented by an object of class Truck).

Problem I have is that I can't figure out how to search through each persons entry for that specific value in an array. I would then take the highest one, push him to the end of the array, or copy to another array and take his name out of the list and continue to the next job.

Also as you can see in the code here, when I define my class Soldier I have to then remap those private variables to public ones. Is there a way I can make those things public in the initialize def, instead of having to define each of them individually?
Thanks for any help.

(This is what I have so far on the new one):


----
#~ We have 5 trucks, in 4 of them (truck 1, 2, 5, and ASV)there are 3 slots, a driver, tc (passenger), and a gunner. in the forth truck (wrecker 1), is just a driver and tc.

#~ The program I'm trying to make needs to do the following:
#~ * Place qualified people in the truck they are qualified in
#~ * Take the extra people once the trucks are filled and give them that day off,
#~ * Cycle the people that are off so that the first person off isn't off again until everyone else has been off.
#~ * Two seperate lists of people for off-days: NCO's and lower-enlisted
#~ * NCO's can only be off wed-sunday, and only one at a time.
#~ * Thursday truck1 and truck2's gunner are off, but "on-call" so will still be slotted in a truck, but will also take an available day-off.
#~ * Sunday is like thursday, but also the team-sergeant (SSG Gib) will be on-call, and will take an nco day-off.
#~ * Every day we need two people on radio duty. On days that no NCOs were off the day before, it will be divided into two shifts, with the lower-enlsted that were off the day before taking
#~ both shifts. On days where an nco other than SSG Gib were off the day before, that NCO will take the whole shift, and two lower enlisted that were off will take the first and second half of the shift.

class Truck #generic truck
def initialize
@dr = nil #driver
@tc = nil #tc (passenger)
@gu = nil #gunner
end

def dr
@dr
end
def dr=(newDr)
@dr = newDr
end

def tc
@tc
end
def tc=(newTc)
@tc = newTc
end

def gu
@gu
end
def gu=(newGu)
@gu = newGu
end

end

class Soldier #personnel and priority
def initialize(name,gun1dr,gun1tc,gun1gu,gun2dr,gun2tc,gun2gu,gun5dr,gun5tc,gun5gu,wrk1dr,wrk1tc,nco)
@name = name
@gun1dr = gun1dr
@gun1tc = gun1tc
@gun1gu = gun1gu
@gun2dr = gun2dr
@gun2tc = gun2tc
@gun2gu = gun2gu
@gun5dr = gun5dr
@gun5tc = gun5tc
@gun5gu = gun5gu
@wrk1dr = wrk1dr
@wrk1tc = wrk1tc
@nco = nco
end
def name
@name
end
def gun1dr
@gun1dr
end
def gun1tc
@gun1tc
end
def gun1gu
@gun1gu
end
def gun2dr
@gun2dr
end
def gun2tc
@gun2tc
end
def gun2gu
@gun2gu
end
def gun5dr
@gun5dr
end
def gun5tc
@gun5tc
end
def gun5gu
@gun5gu
end
def wrk1dr
@wrk1dr
end
def wrk1tc
@wrk1tc
end
def nco
@nco
end
end
#add hadRto? for whether they had rto last day off

#(name,gun1dr,gun1tc,gun1gu,gun2dr,gun2tc,gun2gu,gun5dr,gun5tc,gun5gu,wrk1dr,wrk1tc,nco)
#will only do this the first time to generate text file
team = Array.new

team.push(Soldier.new("SPC Smi",0,0,0,0,0,0,0,1,0,2,0,false))
team.push(Soldier.new("SGT Tho",0,0,0,0,2,0,0,0,0,0,0,true))
team.push(Soldier.new("SPC Sta",0,0,0,0,0,0,1,0,0,2,0,false))
team.push(Soldier.new("PFC Vuk",0,0,0,0,0,0,0,0,0,0,3,false))
team.push(Soldier.new("SGT Edd",0,2,0,0,0,0,0,1,0,1,0,true))
team.push(Soldier.new("PFC Bol",0,0,0,0,0,3,0,0,0,0,0,false))
team.push(Soldier.new("PFC Sip",0,0,0,3,0,0,0,0,0,0,0,false))
team.push(Soldier.new("SPC Est",2,0,0,0,0,0,0,0,0,0,0,false))
team.push(Soldier.new("SPC Fer",2,0,0,0,0,0,0,0,0,0,0,false))
team.push(Soldier.new("PFC Wil",0,0,3,0,0,0,0,0,0,0,0,false))
team.push(Soldier.new("PFC Hou",0,0,0,0,0,0,0,0,2,0,0,false))
team.push(Soldier.new("SPC Wag",0,0,0,0,0,0,1,0,0,0,2,false))
team.push(Soldier.new("SPC And",2,0,0,2,0,0,0,0,0,0,0,false))
team.push(Soldier.new("SPC Sap",0,0,0,0,0,0,0,0,2,0,0,false))
team.push(Soldier.new("PFC Riv",2,0,0,0,0,0,0,0,0,0,0,false))
team.push(Soldier.new("SGT Eld",0,3,0,0,0,0,0,0,0,0,0,true))
team.push(Soldier.new("SPC Ten",0,0,0,0,0,0,2,0,0,0,0,false))
team.push(Soldier.new("SGT Mil",0,1,0,0,0,0,0,2,0,0,0,true))
team.push(Soldier.new("SSG Gib",0,0,0,0,0,0,0,0,0,0,0,false))
team.push(Soldier.new("SGT Min",0,1,0,0,2,0,0,0,0,1,0,true))

#make 2 different arrays for nco and soldier
#import soldier array
#import nco array


#assign off days and RTO duty for nco
#check day
#if day is a sunday, add SSG Gib to off days, he will be on-call
#if day is wednesday through saturday
#pull nco off the front of the list
#add him to days off
#move him to another array
#put last nco on rto duty
#add the remaining to the front of the copy in the same order they are in
#if day is monday or tuesday, add them to main job assignment roster
#take the copy and export it to text file
#end

#assign off days and rto for soldier
#check had rto flag on last soldiers
#if there are more soldiers that had rto than needed
#randomly assign rto to them
#add soldier to rto duty
#set rto flag
#elsif
#add soldier to rto duty
#set rto flag
#end
#end

#check day
#if day is a thursday or sunday
#add gunners as on-call
#add remaining off-days
#elsif day is other
#pull soldiers off the front of array
#put them on off list
#copy array
#end
#add remaining to soldier off roster
#export to text file
#end

#assign remaining workers to main job roster

#assign jobs
#check each truck
#find the person with the highest priority code for each job
#assign them to that job
#if there is more than one person that has the same code, randomly assign them

#export jobs,rto, and off days to csv formatted spread sheet file

I'd have a look at the attr_reader attr_writer methods. They can be called statically and be given a list of symbols, all of whose corresponding value are then accessible simply by calling object.attribute, or object.attribute = x for writable variables.

For (a redundant) example:


class MyClass

attr_reader :myvariable1, :myvariable2
attr_writer :myvariable1, :myvariable2

def initialize(val1, val2)
@myvariable1 = val1
@myvariable2 = val2
end

end


myobj = myClass.new(1,2)
myobj.myvariable1 = 12

puts "Val 1: #{myobj.myvariable1}, val 2: #{myobj.myvariable2}"

OUTPUT:
ruby test.rb
Val 1: 12, val 2: 2


Hope this helps,

Chris

In addition, look at attr_accessor, which does both :)










privacy (GDPR)