|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
How do I fix my problem?
Hi I am new here and programming.
I am making a program for temperature conversion now. I have this for input a temperature(rhtml) <form action = "result" method = "post"> <tr> <td> Celsius to Fahreheit: </td> <td> <input type = "text" id = "CtempF" name = "CtempF" size = "5" /></td></br> <tr/> <input type = "submit" name = "CtempF" value = "Submit" /> <input type = "reset" value = "Clear Form" /> </br> </form> -------------------------------------------------------- And I have a method for conversion(.rb) def result @CtempF=params[:CtempF].to_f if @CtempF > -459.0 @convertedCtoF=((9.0 * @CtempF)/5.0)+32.0 @convertedCtoF=sprintf("%5.1f", @convertedCtoF) elsif @CtempF < -459.0 @convertedCtoF=sprintf(" 'INVALID! THE NUMBER SHOULD BE GREATER THAN -459'") else @convertedCtoF=sprintf(" 'INVALID! SHOULD BE NUMERIC'") end end .................................................... Finally I have this for the output the result(.rhtml) Celsius to Fahreheit is: <%= @convertedCtoF %> <br/> If I enter 22 into the textbox, it works fine. If I enter -666 into the textbox, it also works fine. But if I enter "aaa", the output shows "Celsius to Fahreheit is: 32.0". I don't know where the 32.0 comes from, and how to fix it. Could anybody teach me how to fix it? Thank you |
|
#2
|
|||
|
|||
|
please learn to use code tags; they make your post infinitely more readable.
The reason this is happening can be described with an example: Code:
irb(main):001:0> s = "aaa".to_f => 0.0 So your check passes... You can do a couple of things to avoid this, but the following is probably how I would go about it Code:
def foo(v)
begin
Float(v)
rescue
"Invalid Argument"
end
end
And the result: Code:
irb(main):020:0> foo 123 => 123.0 irb(main):021:0> foo "123" => 123.0 irb(main):022:0> foo "aaa" => "Invalid Argument" Realize that usually you could do something along the lines of object.kind_of? Numeric, but since you know you are getting a string, that would never work. There might be a more elegant solution, but off the top of my head, I am not coming up with it...
__________________
-- I'll provide you with reference points; if they dont work, refer to something else. My personal site: Basic geek randomness |
|
#3
|
|||
|
|||
|
Hi, thank you for your reply.
I understand why my program outputted the number after I entered the string. And I tried to modify my code. But I cannot change it. Could you please show me how to change the result method? Thank you |
|
#4
|
|||
|
|||
|
Quote:
I'm not sure what you mean by this. Do you mean that you can not edit the file? If you can edit the file, what is preventing you from changing the code? |
|
#5
|
|||
|
|||
|
Quote:
I don't know how to do like def result(v) ----- ----- ----- end and how to use "v" inside the method. Thank you |
|
#6
|
|||
|
|||
|
This really breaks down to the basics of programming (not just with Ruby).
If you have access to irb (the ruby interactive environment) I would suggest that you try to write some things there as you gain an understanding of just how they work. To help with how to use function calls and arguments to those functions, try the following goolge terms: subroutine, function, method, procedure, subprogram The first one there should lead you direct to wikipedia which will allow you to explore further. Hope that helps... |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Ruby Programming > How do I fix my problem? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|