How to solve program?

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.

I posted this question in Stackoverflow But I am not getting answer.

On Tue, Feb 11, 2014 at 11:20 AM, Jaimin P. [email protected]
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
(http://ruby-doc.org/core-2.1.0/String.html#method-i-to_i), and you
need a second variable to store this number and check x against it.

Hope this gets you in the right direction,

Jesus.

Could you write modify program? please.

Joel P. wrote in post #1136352:

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.

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.

Am 13.02.2014 19:29, schrieb Jaimin P.:

Is this program correct? Could you please tell me?

Did you try whether it works?

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

Jesús Gabriel y Galán wrote in post #1136295:

On Tue, Feb 11, 2014 at 11:20 AM, Jaimin P. [email protected]
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
(http://ruby-doc.org/core-2.1.0/String.html#method-i-to_i), 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?

unknown wrote in post #1136598:

Am 13.02.2014 19:29, schrieb Jaimin P.:

Is this program correct? Could you please tell me?

Did you try whether it works?

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

Yes, I tried to execute the program. Execution is possible.

I was asked because I want to know Is it correct way of programming or
not?

I really recommend you read a tutorial on Ruby, the Ruby P.ming
Guide, Chris P.'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.

List-Unsubscribe: mailto:[email protected]?body=unsubscribe

On Fri, Feb 14, 2014 at 3:28 AM, Jess Gabriel y Galn <

On Thu, Feb 13, 2014 at 7:29 PM, Jaimin P. [email protected]
wrote:

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 http://ruby-doc.org/core-2.0.0/Kernel.html#method-i-gets
    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 P.ming
Guide, Chris P.'s Learn to Program (http://pine.fm/LearnToProgram/)
or some other introduction to the language.

Hope this helps,

Jesus.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs