How to ask a number in Ruby? [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 9 years ago.
Improve this question
I want to make a program like,ask a number and print 1 to number by using gets and by using loop. So,I am asking about gets and how to do program which is I given below as program title.
How to ask a number by using gets?If possible explain me with example.
By using gets,I want to print 1 to number. My program titleis Ask a number and print 1 to number by using Ruby.
How can I solve that program?Please help me on this.

As Arup, suggested use Kernel#gets to capture a user input from terminal. The remaining bit can be simply done with a for loop:
num = gets.to_i #Convert the user input to integer
for i in 1..num
puts i
end
You can further modify this to suit your need.

Do as below using Kernel#gets. #gets will give you a string, then to convert the number string to a number use String#to_i.
number = gets.to_i
If I want to make program which from 1 to number then what should I do?
Use a Range then.
(1..number).each do |n|
# code
end

Related

How to get all decimal points in in ruby? ( HOLD) [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
The following shows that when I use the to_f method to covert a string to a floating point number and the last decimal point is dropped. How can preserve all decimal points in a given number?
irb(main):002:0> value='1.7.8'
=> "1.7.8"
irb(main):003:0> value.to_f
=> 1.7
Some context:
I am writing the the value to a file and If I write it as a string I get the quotes '1.7.8'. What I am looking for infact is 1.7.8. Hope that makes sense.
EDIT:
I see the error in my question so I'm trying to close it however I can only vote to close it.
just to clarify what I've found is actually contrary to what I said above.
turns out if I write the string '1.7' to a file it is written as '1.7' but with the string '1.7.8' it is written as 1.7.8. I'm just trying to understand why this is occurring.
To write it to a file simply write it like so:
value = "1.7.8"
File.open("file") { |f| f.puts("#{value}") }
The string in the file will not have quotes around it.

Array within array ruby [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 have an array within an array. I can call a certain object by doing this...
array[1][2]
I would like to be able to insert a variable instead of one of these values. Maybe something like...
array[#{variable1}][#{variable2}]
Is this possible?
Just replace the numeric literal with the variable:
variable1 = 1
variable2 = 2
array[variable1][variable2]
What you mentioned is known as string interpolation, but can only be used in quotes.
For example, say you have a variable defined, price
price = 80
You can say something like this
puts "The price is $#{price}"
This will translate to The price is $80
It's not possible to do that in arrays, but it is possible to substitute values inside
a = 7
b = 9
array[a] will get the 8th value of the array, whereas array[b] will get the 10th value of the array.
Hope this helps.

Something similar? [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'm in my first year and I have to do some problems in a new programming language invented by my teacher which is very similar with java
The problem is: Write a program that asks the user to enter a number. It will then repeatedly display the message "hello" twice the number of times as the number entered. So if you entered 3 it should print "hello" 6 times.
So far I did this:
get number
set p=number*2
for p=1 to 100
repeat
display "hello"
until number !=p
endfor
What am I doing wrong ?
get number
set p=number*2
for i=1 to p
display "hello"
endfor

Anyone can comment this ruby code? [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'm a total novice in ruby, i came across this code in an article about a bug in gmail:
(0..0xFFFFFFFFFF).each do |i|
puts "#{"%010X" % i}"
end
it is supposed to generate an dictionary, but i can't figure out how it works
Thank You all!
The code iterates and prints all values from 0 to 0xFFFFFFFFFF Similar to how
(1..10).each do |i|
puts i
end
iterates and prints all values from 1 to 10.
For each value between 0 and 0xFFFFFFFFFF it simply prints out its current hex value:
0000000000
...
0000005E6A
0000005E6B
0000005E6C
0000005E6D
0000005E6E
0000005E6F
...
FFFFFFFFFF

Making a Python3 Variable Using a Counter [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 have some BASH code that I want to convert to Python3.
INPUTINDEX$INPUTCOUNTER="$INPUT"
Every time a user types an input the counter (code not shown) increases one number. With this code, the user's second input would be in variable INPUTINDEX2. INPUT, of course, is the user's input. The INPUTCOUNTER is a counter that increases each time an input is entered.
The question: How can I do this in Python3.3?
I suggest just appending to a list and the number of entries in the list is your counter.
#!/usr/bin/env python
def main():
entries=[]
while True:
entries.append(raw_input('input something: '))
if not len(entries[-1]):
entries=entries[:-1]
break
counter=len(entries)
print 'counter %d, entries %s.' % (counter, entries)
if (__name__ == "__main__"):
main()

Resources