I am following the Rails Tutorial by Michael Hart and I am already on Chapter 7. But I wanna do something different right now, which the tutorial doesn't teach. I wanna run a script file inside my webpage. How I can do that? I saw other posts here saying to use Sinatra, but since I am following this tutorial I don't think it is such a good idea to use it because it can make everything different from the tutorial.
Here is the simple script I wanna run on my webpage:
#Somando idades
def soma_vetor_excluindo(index,vet)
soma = 0
for i in 0..9
if(i!=index)
soma = soma + vet[i].to_i
end
end
return soma
end
def soma_vetor(vet)
soma = 0
for i in 0..9
soma = soma + vet[i].to_i
end
return soma
end
def maior_vetor(vet)
maior = 0
for i in 0..9
if(maior < vet[i])
maior = vet[i]
end
end
return maior
end
idades = (0..9).collect{rand(99)+1}
soma_idades = (0..9).collect{0} soma = 0
print "#{idades} \n"
for i in 0..9
soma_idades[i] = soma_vetor_excluindo(i,idades)
end
print "#{soma_idades} \n"
div = soma_vetor(soma_idades) / 9
resp = div - maior_vetor(soma_idades)
puts "#{resp}"
-
for i in x isn't the 'Ruby' way. You'll want to change it to (0..9).each { |i| my_method(i) } or "do/end" if it's multiline.MCB– MCB2014年02月12日 18:21:02 +00:00Commented Feb 12, 2014 at 18:21
-
Also, look at Ruby's inject method for summing an array and for getting the highest value you can arr.sort{|x,y|y<=>x}.first Now, where do you want the output from print and puts to show up? If you want to program client side (in the users' browser) you use javascript.MCB– MCB2014年02月12日 18:46:36 +00:00Commented Feb 12, 2014 at 18:46
2 Answers 2
The simplest way to do it would be to make the method soma_vetor_excluindo, soma_vetor, maior_vetor, etc, controller methods, so when you send data through a form or ajax, the action would trigger, calculate the values and return you a result.
Knowing this, you can have a controller, let's say MathController.rb, and inside it, the soma_vetor_excluindo method:
class MathController < ApplicationController
def soma_vetor_excluindo
end
def soma_vetor
end
def maior_vetor
end
end
To access this, you probably need a route, so on your routes.rb add something like this:
get 'math/soma_vetor_excluindo/:index/:vet', to 'math#soma_vetor_excluindo'
get 'math/soma_vetor/:vet', to 'math#soma_vetor'
get 'math/maior_vetor/:vet', to 'math#maior_vetor'
This means that when your browser hit localhost/math/soma_vetor_excluindo/1/2 or the other urls, it would send a get request to the controller calling the soma_vetor_excluindo method and putting in the parameters, params[:index] and params[:vet], so theoretically the script would run.
The thing is, you can adapt your controller to do something like this with very little work.
4 Comments
calculate just to simplify things. You can call it soma_vetor_excluindo. Lemme edit it.def minigame @title = "2011question6" end I've created the MathController as you said. But if I insert what you said to routes.rb my website breaks. So I don't know what to put on routes.rb. Right now I'm only with the static page on routes.rb match '/2011question6page', to: 'static_pages#2011question6page', via: 'get'admin/:service.:action => "admin#service" so if the authenticated user goes to something like admin/mailer.restart it will call my admin controller's service action. From there, I put the logic to stop, start, restart, or whatever the specified service.I believe the simplest solution is to load a page per script. First you add a path for your script into the routes.rb with something like:
get 'scripts/your_script', to 'scripts#your_script
And in the controller (app/scripts_controller.rb) you should add your code like this:
class ScriptsController < ApplicationController
#Somando idades
def soma_vetor_excluindo(index,vet)
soma = 0
for i in 0..9
if(i!=index)
soma = soma + vet[i].to_i
end
end
return soma
end
def soma_vetor(vet)
soma = 0
for i in 0..9
soma = soma + vet[i].to_i
end
return soma
end
def maior_vetor(vet)
maior = 0
for i in 0..9
if(maior < vet[i])
maior = vet[i]
end
end
return maior
end
def your_script
idades = (0..9).collect{rand(99)+1}
soma_idades = (0..9).collect{0}
soma = 0
answer = "#{idades} \n"
for i in 0..9
soma_idades[i] = soma_vetor_excluindo(i,idades)
end
answer << "#{soma_idades} \n"
div = soma_vetor(soma_idades) / 9
resp = div - maior_vetor(soma_idades)
answer << "#{resp}"
render(text: answer)
end
end
when you access the page scripts/your_script, it should render a plain text presentation of your script result.
Although this is not the most elegant solution, it should solve your problem.