How can i make this procedure into a function?

Hello _everybody in the Ruy Community,

    def pause (dauer)

tn=Time.now.to_i
te=tn+dauer
  while tn<te do
    tn=Time.now.to_i
  end

end

The Problem here is that when I use this procedure in my programm all
the separate parts happen at the same time and i do not see the results
in between.

could anybody help me here?

This is a busy loop and should not be used unless you are doing much
more low level stuff.

Take a look at Kenel#sleep(amount)

Regards,

Markus

Akvile K. wrote in post #1036147:

what i need is that this procedure works in a programm that chsnges the
lights in a traffic light. i am using timer now but i am 100% sure that
my professor during the exam wants to see a function or a procedure in
this programm.

If that’s really the case then your professor doesn’t know much about
ruby. Ruby has neither procedures nor functions - it has methods. (You
could count lambdas as being another type of callable too)

In some imperative languages, the only difference between a procedure
and a function is whether it returns a value or not. So by that
definition, a method in ruby will substitute for either.

Now, if you are going to define a method, I suggest you do it outside
a block. e.g.

def mymethod(x,y,z)

end

Shoes.app do
… all this is a block
mymethod(a,b,c)
… etc
end

This is because a block is essentially a bit of callback code, to be
executed zero, one or more times by the method it is passed to
(Shoes.app here). So if you use ‘def’ within a block, you may end up
defining the same method lots of times.

Hey,

what i need is that this procedure works in a programm that chsnges the
lights in a traffic light. i am using timer now but i am 100% sure that
my professor during the exam wants to see a function or a procedure in
this programm. any suggestions?

Shoes.app :titel=> Ampel, :height=> 500, :width=> 500 do
background white

#Ausgangssituation

#Fußgängerampel
fill gainsboro
rect 10,50,90,130
fill black
lighta = oval :top=> 60, :left=> 30, :radius=> 25
fill black
lightb = oval :top=> 120, :left=> 30, :radius=> 25

#Autoampel
fill gainsboro
rect 210,45,150,300
fill black
light1 = oval :top=> 50, :left=> 245, :radius=> 40
fill black
light2 = oval :top=> 150, :left=> 245, :radius=> 40
fill black
light3 = oval :top=> 250, :left=> 245, :radius=> 40

#def pause (dauer) Prozedur, welche die Zeitabstände zwischen den
Ampelphasen angeben sollte
#tn=Time.now.to_i
#te=tn+dauer
#while tn<te do
#tn=Time.now.to_i
#end
#end

stack :margin=> 30, :top=> 150 do
button “Walk” do

#Situation 1 : knopf gedrückt

#fuflg‰ngerampel

timer (5) do

lighta.fill = red
lightb.fill = black

#Autoampel

light1.fill = black

light2.fill = black

light3.fill = green

end

#Situation 2 : knopf gerdückt 2

#fußgängerampel

timer (15) do

lighta.fill = red
lightb.fill = black

#Autoampel

light1.fill = black
light2.fill = orange
light3.fill = black

end

#Sitation 3 : knopf gedrückt 3, fußgänger laufen über das zera
streifen

#fußgängerampel

timer (25) do

lighta.fill = black
lightb.fill = green

#Autoampel

light1.fill = red
light2.fill = black
light3.fill = black

end

#Situation 4 : Ampel geht aus 1, Autos dürfen gleich fahren

#fußgängerampel

timer (30) do

lighta.fill = black
lightb.fill = green

#Autoampel

light1.fill = red
light2.fill = orange
light3.fill = black

end

#Sitaution 5 : Grüne ampel läuft eim Fußgäger aus, Autos dürfen fahren

#Fußgängerampel

timer (35) do

lighta.fill = red
lightb.fill = black

#Autoampel

light1.fill = black
light2.fill = black
light3.fill = green

end

#Situation 6 : Alle ampeln gehen aus

#Fußgängerampel

timer (40) do

lighta.fill = black
lightb.fill = black

#Autoampel

light1.fill = black
light2.fill = black
light3.fill = black

end

end

end

end

Hi Akvile,

i am using timer now but…
I think it’s better to use timer() method in this case.
But if you have to write your own method, use Thread.new().
A sample snippet is this:

Shoes.app do
def pause t
Thread.new do
sleep t
yield
end
end

light = oval 100, 100, 100
pause(3){light.fill = red}
pause(5){light.fill = blue}

end

ashbb

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs