Ruby grammar problem . I don't know how to solve it [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 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.

Related

Can't output the result in after if [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 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).

Regex dynamic string manipulation [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 6 years ago.
Improve this question
Im aiming for a regex formula to return chunks of a string based on a character, if this string contains L1 then its going to be only one chunk, if L2 is found it would return 2 chunks, L3 = 3 chunks.
Example
Lets assume we have this string
"L2N1N1"
and we would like to get 2 string
"L2N1" and "L2N1N1"
Another example
"L3N1N1N2"
to return 3 strings
"L3N1" "L3N1N1" "L3N1N1N2"
Im using Ruby
"L3N1N1N2".sub(/L(\d)(?:N\d)+/) do |m|
$1.to_i.times.map { |i| m[0..3+2*i] }.join(' ')
end
#⇒ "L3N1 L3N1N1 L3N1N1N2"

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

Sorting arbitrary numeric data and leave whitespaces for the missing values [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 need to sort the numeric data in a row/column while leaving space of missing values at scale of 1-5.
Like:
A B
13245 1_2_3_4_5
152.... 1_2_ _ _5
Try this UDF:
Function ModSort(Str As String) As String
Res = ""
For Iter = 1 To 5
If InStr(1, Str, CStr(Iter)) Then
Hold = CStr(Iter) & "_"
Else
Hold = " _"
End If
Res = Res & Hold
Next Iter
Res = Left(Res, 9)
ModSort = Res
End Function
Screenshot:
Let us know if this helps.
Not meant as a serious answer (UDF seems good enough) but appears possible with a formula:
=IF(LEN(SUBSTITUTE(A2,1,""))<LEN(A2),"1_"," _")
&IF(LEN(SUBSTITUTE(A2,2,""))<LEN(A2),"2_"," _")
&IF(LEN(SUBSTITUTE(A2,3,""))<LEN(A2),"3_"," _")
&IF(LEN(SUBSTITUTE(A2,4,""))<LEN(A2),"4_"," _")
&IF(LEN(SUBSTITUTE(A2,5,""))<LEN(A2),"5",)

Ruby: How do I get items out of an array? [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 8 years ago.
Improve this question
I have this:
array = ["a","b","c"]
How do I get this:
"a","b","c"
I need to get the items out of the array, each double-quoted, separated by a comma.
array.collect { |a| "\"#{a}\"" }.join(",")
I'm just started to learn ruby, I guess:
return ["a","b","c"].map{|i| '"' + i + '"'}.join(",")
Might you want to get this:
irb(main):009:0> [1, 2, 3].map(&:to_s).join('","')
=> "1\",\"2\",\"3"
"a","b","c" this is not abject (these are 3 objects). But in ruby any code returns object value. So you should know what you want to get: 1 object (I returning string in this example) or various. If you want to get 3 objects you should extract array like this:
a, b, c = [1,2,3].map(&:to_s)

Resources