3

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}"
2
  • 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. Commented 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. Commented Feb 12, 2014 at 18:46

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

How I can make "soma_vetor_excluindo" a controller method?
I used calculate just to simplify things. You can call it soma_vetor_excluindo. Lemme edit it.
First of all, thanks for helping me out! But I still can't figure out how to solve this. Maybe I am too newbie.Here is how it is looking right now: I created a static page for the script called "2011question6" So on my static_pages_controller.rb I got 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'
I do this when I need to access certain services on my server. In my case, I have urls like 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.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.