Can't output the result in after if [closed] - ruby

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).

Related

Ruby grammar problem . I don't know how to solve it [closed]

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.

Regular expression related to credit card [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I would like to validate master card for New series The BIN range (222100-272099). currently I have Regular expression for existing mastercard series...5[1-5][0-9]{14}
Edit : based on the comment of Kul-Tigin
When working with numbers this may be easier :
Title = "Valid card number or not"
Do
CardNumber = InputBox("Please type your card number ",Title,"222100")
If IsValidcard(CardNumber) = True Then
MsgBox CardNumber & " The Card number is valid !",64,Title
Else
MsgBox CardNumber & " The Card number is not valid !",16,Title
End if
Loop Until CardNumber = ""
'**********************************************************************
Function IsValidCard(Num)
IsValidCard = False
Set regEx = New RegExp
regEx.Pattern = "^\d{6}$"
If regEx.Test(Num) then
x = CDbl(num)
if x >= 222100 AND x <= 272099 Then
IsValidCard = True
Else
'not in range
End If
Else
'not 6 digits
end if
End Function
'**********************************************************************

If input is "X", multiply by "Y" [closed]

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

Random numbers in Ruby [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am trying to make n random numbers from -0.5 from to 0.5
and I made a function like this
def create_noise(n)
end
I found an implementation of this but i don't think this works
randoms = Set.new
loop
randoms << rand(max)
return randoms.to_a if randoms.size >= n
You would just do
def create_noise(n)
n.times.collect { rand(-0.5..0.5) }
end
that will spit back an array like this:
[-0.034680737617880486, 0.34802029078157803, 0.1346483808607455, 0.12155616615186282, -0.41043213731234474]

Ruby: how to make an array of words, with the length of the array being random [closed]

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

Resources