Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm trying to do a simple math operation, but I'm stuck.
Ask user to introduce an amount of money:
puts "Capital to deposit"
capital = gets.chomp
Ask user to introduce a fixed number of days:
puts "Indicate days of deposit"
deposit_time = gets.chomp
I made the following array with the only possible answers for step #2:
deposit_time = [30, 45, 60]
I need a multiplication operation that depends on what the user chooses in step #2. For instance, let's suppose the user chooses 30. I would need the code to read:
capital = 10
deposit_time = 30
to multiply 10 * 1.0219 and print the resulting number: 10.219. Any ideas?
Use a hash, not an array.
deposit_rates = { 30 => 1.0219, 45 => 1.0336, 60 => 1.0467 }
deposit_time = gets.to_i
deposit_rate = deposit_rates[deposit_time]
# will be `nil` if not one of the defined ones
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Please output the number obtained by adding a and b.
At the end of the line break, do not include extra characters, blank lines.
expmple1
1 1
result1
2
expmple2
0 99
result2
99
My code is:
input_lines = gets.chop
a = input_lines[0]
b = input_lines[1]
puts a + b
But it's not working, please help.
Add split to the chopped gets would work:
input_lines = gets.chop.split
a = input_lines[0].to_i
b = input_lines[1].to_i
puts a + b
Try it.
Check String doc.
More DRY way to do it:
input_lines = gets.chop.split.map(&:to_i)
a,b = input_lines
puts a + b
In this case the numbers inside input_lines already changed to Integers.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I want to make this simple program that returns the weight quantity by choosing the units. When I run the program it runs with no problems and asks the two questions in if but doesn't return the value
Here is my code:
puts "What is your starting weight and ratio unit?
1:kg.m/h
2:gm.m/s
3:mm.m/s"
inputing_unit = gets.chomp
puts "What is your ending weight and ratio unit?
1:kg.m/h
2:gm.m/s
3:mm.m/s"
ending_unit = gets.chomp
if inputing_unit == 1 and ending_unit == 1 then
puts "What is your weight?"
input_weight = gets.chomp.to_i
puts "What is your ratio?"
input_ratio = gets.chomp.to_i
puts "Your moving value is #{input weight * input_ratio}"
end
As I can see from that screenshot in the beginning of the description on pre-last line where is your puts statement there is a typo #{input weight * input_ratio}. You should change ...#{input weight... to ...#{input_weight... (with a dash).
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I use the excellent faker gem to generate random words for my models. Eg. product.name = Faker::Lorem.word
Sometimes I need to generate a sentence, and I want the length of the sentence to
vary each time.
How to achieve this with ruby?
How about:
result = rand(max_size).times.map { produce_word }
Since you have not provided enough information, this is my approach, [*1..100].sample will return a random number between 1 and 100, so looping that times the string which is returned bya method named get_word will get stored in the array word_array
word_array = []
[*1..100].sample.times do
word_array << get_word
end
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm working on a project in RMXP with Ruby scripts.
I create a table with id maps. I am now looking to go the table such that if the player enters one of the maps of the table, things happen.
Example of action: tMaps = [006, 008, 009]
If the hero is on the map 004, 005 ... it nothing happens If the hero
is on the map 006, Action
I hope this is feasible. If not, what other means I have in Ruby to find the maps and to assign actions?
PS I've traveled a bit, this wiki : http://fr.wikibooks.org/wiki/Programmation_Ruby/Contr%C3%B4le
Your list is wrong, I suppose you want integer : [6, 8, 9] not [006, 008, 009] (this is octal form)
tMaps = [6, 8, 9]
tMaps.each do
|value| puts 'value : '+value.to_s
end
# or :
for value in tMaps
puts 'value : '+value.to_s
end
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I have a method that will return a number between 1 - 100
depending on whether the response (x) is between
100 - 90 i would like one response, 89 - 85 another 84 - 72 another ... etc
I use
if x > 90
response a
elsif x > 85
response b
elsif etc...
but this seems a little messy, is there a better way of refactoring this?
Many thanks.
Take advantage of Range#=== and use a case statement:
case x
when 72..84
# Do something
when 85..89
# Do something
when 90..100
# Do something
else
# Do something when no matches
end
You could try a table (a Hash) where the keys are ranges and the values ar the numbers you want to return :
T = {
(90..100) => 1,
(85..89) => 2,
# and so on
}
(r,v) = T.find {|r,v| r.member? x}
if v then
return v
else
# x wasn't in any of the defined ranges
end