What you need to do is to change the 10 in your first program by a
number entered by the user. So look into Kernel#gets
(Module: Kernel (Ruby 2.1.0)) and how to
convert what the user enters into a number String#to_i
(Class: String (Ruby 2.1.0)), and you
need a second variable to store this number and check x against it.
Hope this gets you in the right direction,
Jesus.
···
On Tue, Feb 11, 2014 at 11:20 AM, Jaimin Pandya <lists@ruby-forum.com> wrote:
First I done program like Write a program using Ruby and Print 1 to 10.
This program
I solved as follow:
x = 0
while x <= 10
puts x
x = x + 1
end
Now I want to do another program. I give program statement as follow.
My program statement is Write a program using Ruby ask a number, and
Print 1 up to number.
I solved this program by following :
x = 0
while x <= 10
if x.class == Fixnum
puts x
end
x = x + 1
end
But this solution of program is wrong.
So How to solve this program? Please help me on this.
If you don't learn to read documentation and do your own research and
experimentation, you won't learn anything.
If you really want to learn programming, get your hands dirty. You've
already been pointed in the right direction.
If you don't learn to read documentation and do your own research and
experimentation, you won't learn anything.
If you really want to learn programming, get your hands dirty. You've
already been pointed in the right direction.
Yes, that's right. I am trying to do experimenting and I will read
documentation also.
So How to solve this program? Please help me on this.
What you need to do is to change the 10 in your first program by a
number entered by the user. So look into Kernel#gets
(Module: Kernel (Ruby 2.1.0)) and how to
convert what the user enters into a number String#to_i
(Class: String (Ruby 2.1.0)), and you
need a second variable to store this number and check x against it.
Hope this gets you in the right direction,
Jesus.
I solved the program by following :
x = 0
y = gets(10)
while x <= y.to_i
puts x
x = x + 1
end
Is this program correct? Could you please tell me?
···
On Tue, Feb 11, 2014 at 11:20 AM, Jaimin Pandya <lists@ruby-forum.com> > wrote:
Answer: Yes (as long as the number does not have more than 10 digits).
And instead of while I would use e.g. Integer#upto.
Regards,
Marcus
···
Am 13.02.2014 19:29, schrieb Jaimin Pandya:
Jesús Gabriel y Galán wrote in post #1136295:
On Tue, Feb 11, 2014 at 11:20 AM, Jaimin Pandya <lists@ruby-forum.com> >> wrote:
puts x
end
x = x + 1
end
But this solution of program is wrong.
So How to solve this program? Please help me on this.
What you need to do is to change the 10 in your first program by a
number entered by the user. So look into Kernel#gets
(http://ruby-doc.org/core-2.1.0/Kernel.html#method-i-gets\) and how to
convert what the user enters into a number String#to_i
(Class: String (Ruby 2.1.0)), and you
need a second variable to store this number and check x against it.
Hope this gets you in the right direction,
Jesus.
I solved the program by following :
x = 0
y = gets(10)
while x <= y.to_i
puts x
x = x + 1
end
Is this program correct? Could you please tell me?
Well, as others have said, first is to check if the program actually
does what you want. It prints from 0 to the number you type, included.
Is this what you want?
Apart from that there are a couple of things that could be made
better, and also a completely different way of doing it, which would
be more rubyish:
- You are calling to_i many times, every time you check the loop
condition. Better like this:
y = gets.to_i
- In Ruby, generally, internal iterators are preferred over external ones:
y = gets.to_i
1.upto(y) {|n| puts n}
You can do inside the block what you need with n.
Or if you only want to print it:
y = gets.to_i
puts *(1..y)
I really recommend you read a tutorial on Ruby, the Ruby Programming
Guide, Chris Pine's Learn to Program (http://pine.fm/LearnToProgram/\)
or some other introduction to the language.
Hope this helps,
Jesus.
···
On Thu, Feb 13, 2014 at 7:29 PM, Jaimin Pandya <lists@ruby-forum.com> wrote:
Jesús Gabriel y Galán wrote in post #1136295:
On Tue, Feb 11, 2014 at 11:20 AM, Jaimin Pandya <lists@ruby-forum.com> >> wrote:
puts x
end
x = x + 1
end
But this solution of program is wrong.
So How to solve this program? Please help me on this.
What you need to do is to change the 10 in your first program by a
number entered by the user. So look into Kernel#gets
(http://ruby-doc.org/core-2.1.0/Kernel.html#method-i-gets\) and how to
convert what the user enters into a number String#to_i
(Class: String (Ruby 2.1.0)), and you
need a second variable to store this number and check x against it.
Hope this gets you in the right direction,
Jesus.
I solved the program by following :
x = 0
y = gets(10)
while x <= y.to_i
puts x
x = x + 1
end
Is this program correct? Could you please tell me?
On Fri, Feb 14, 2014 at 3:28 AM, Jesús Gabriel y Galán < jgabrielygalan@gmail.com> wrote:
On Thu, Feb 13, 2014 at 7:29 PM, Jaimin Pandya <lists@ruby-forum.com> > wrote:
> Jesús Gabriel y Galán wrote in post #1136295:
>> On Tue, Feb 11, 2014 at 11:20 AM, Jaimin Pandya <lists@ruby-forum.com> > >> wrote:
>>>
>>> puts x
>>> end
>>> x = x + 1
>>> end
>>>
>>> But this solution of program is wrong.
>>>
>>> So How to solve this program? Please help me on this.
>>
>> What you need to do is to change the 10 in your first program by a
>> number entered by the user. So look into Kernel#gets
>> (Module: Kernel (Ruby 2.1.0)) and how to
>> convert what the user enters into a number String#to_i
>> (Class: String (Ruby 2.1.0)), and you
>> need a second variable to store this number and check x against it.
>>
>> Hope this gets you in the right direction,
>>
>> Jesus.
>
> I solved the program by following :
>
> x = 0
> y = gets(10)
> while x <= y.to_i
> puts x
> x = x + 1
> end
>
> Is this program correct? Could you please tell me?
Well, as others have said, first is to check if the program actually
does what you want. It prints from 0 to the number you type, included.
Is this what you want?
Apart from that there are a couple of things that could be made
better, and also a completely different way of doing it, which would
be more rubyish:
- If you check Module: Kernel (Ruby 2.0.0)
you'll see that passing an integer to gets means it will read at most
those many characters from the input. Is that what you wanted?
- You are calling to_i many times, every time you check the loop
condition. Better like this:
y = gets.to_i
- In Ruby, generally, internal iterators are preferred over external ones:
y = gets.to_i
1.upto(y) {|n| puts n}
You can do inside the block what you need with n.
Or if you only want to print it:
y = gets.to_i
puts *(1..y)
I really recommend you read a tutorial on Ruby, the Ruby Programming
Guide, Chris Pine's Learn to Program (http://pine.fm/LearnToProgram/\)
or some other introduction to the language.
I really recommend you read a tutorial on Ruby, the Ruby Programming
Guide, Chris Pine's Learn to Program (http://pine.fm/LearnToProgram/\)
or some other introduction to the language.
Hope this helps,
Jesus.
Yes, your answer is very much helpful for me. I get my answer from your
reply. Thank you.