1 Replies - 2159 Views - Last Post: 30 January 2016 - 06:07 PM Rate Topic: -----

#1 Kahnmir  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 28-January 16

Questions about .chomp, .to_i, .to_f, and simplfying code

Posted 30 January 2016 - 06:01 PM

Hello, I recently started teaching myself Ruby.

I've been trying to take a bit of a hands on approach, so I figured I'd write a program in Ruby
that I'd written previously in a C++ class I took awhile ago.

The program is meant to take a integer and find all the factors of said integer, and print them on the screen.

I didn't have much trouble writing the program (which is at the bottom of the post), there are no errors, and it does what i want it to do: factor big numbers into little numbers, and put them on the screen.

but it is a bit spartan, and I have the distinct feeling I did some unnecessary things, but not the unnecessary things that I want it to do.

first off,

currently the program outputs the factors one at a time, so that it look like this:

2
2
5
7

but I want it to output them all on one line like this: 2, 2, 5, 7

I thought maybe I could do it by using .chomp on my puts lines, but that didn't work, which is confusing because I thought .chomp was meant to prevent extra lines like that; the tutorial I am using said it " It takes off any Enters hanging out at the end of your string." so I thought it might work in a similar situation, but that seems to be wrong, which begs the question, is getting rid of enters really all that .chomp does?

and how do I get rid of those extra line so that the numbers output the way I want them too?

second question,

I ended up using .to_i on my variables any time my program did math, Ruby seems kinda picky about what state a variable is in, which is understandable, but what I am confused about is; when I use the .to_i or .to_f methods, does that convert the variable to that state permanently or just for that line?

Anyway, I hope these questions aren't too annoying, but advice would be greatly appreciated!



puts 'Enter number to be factored'
inum = gets.chomp
prime = 1
puts 'the factors are: '

primetest = inum.to_i % 2

while primetest == 0

#this section continuously divides by 2 until it
# runs into a prime number, if the input number cannot be divided
# by 2 it is skipped.

	inum = inum.to_i / 2
	puts '2'
	primetest = inum.to_i % 2

end


while (primetest.to_i >= 1 )

# This section finds the lowest prime number of the remainder of part 1, 
# then prints it on the screen, and repeats 
# until there are no more prime numbers to factor out.

		prime = prime.to_i + 2	
		primetest = inum.to_i % prime.to_i
		if primetest == 0

			puts prime.to_s
			inum = inum.to_i / prime.to_i
			prime = 3
			primetest = inum.to_i % prime.to_i

			if inum == 1
				primetest = 0
			end
				
		end

end 




Is This A Good Question/Topic? 0
  • +

Replies To: Questions about .chomp, .to_i, .to_f, and simplfying code

#2 xclite  Icon User is offline

  • I wrote you an code
  • member icon


Reputation: 1237
  • View blog
  • Posts: 4,029
  • Joined: 12-May 09

Re: Questions about .chomp, .to_i, .to_f, and simplfying code

Posted 30 January 2016 - 06:07 PM

View PostKahnmir, on 30 January 2016 - 08:01 PM, said:

but I want it to output them all on one line like this: 2, 2, 5, 7

I thought maybe I could do it by using .chomp on my puts lines, but that didn't work, which is confusing because I thought .chomp was meant to prevent extra lines like that; the tutorial I am using said it " It takes off any Enters hanging out at the end of your string." so I thought it might work in a similar situation, but that seems to be wrong, which begs the question, is getting rid of enters really all that .chomp does?

chomp only removes newlines at the end of a string. The string you're sending to puts doesn't have a newline. "puts" works by printing what you send it, with a newline afterwards. Therefore, calling chomp will never change the output of puts.

Try "print".

Quote

I ended up using .to_i on my variables any time my program did math, Ruby seems kinda picky about what state a variable is in, which is understandable, but what I am confused about is; when I use the .to_i or .to_f methods, does that convert the variable to that state permanently or just for that line?

to_i or to_f don't modify the variable you call them on. They return a new variable.
a = "23"
b = a.to_i + 34
puts a # 23
puts b # 57


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1