I’m brand new to ruby, as in, one hour! So along with perhaps
answering my very basic question, please feel free to point me to other
resources on the web
How do I fix the following so that entering ‘q’ indeed breaks out of
the loop? I’ve read the following but think I will need to see an
example to truly understand.
Thanks in advance
BTW I always learn new language basics by figuring out how to convert
to roman numerals
arabic = ’ ’
while arabic != ‘q’ do
puts “Enter a whole number from 1 to 3999 for conversion”
puts “to Roman Numerals (enter ‘q’ to exit)”
arabic = gets
end
How do I fix the following so that entering ‘q’ indeed breaks out of
the loop? I’ve read the following but think I will need to see an
example to truly understand.
Gets also reads in the end-of-line separator. You could use:
No problem. You may also find the resources at http://www.ruby-doc.org/ useful, if you haven’t checked that site out
already. Specifically, the online version of “Programming Ruby”, known
affectionately around here as the Pickaxe (because of its cover). Its
reference on ‘gets’ may have explained the problem (and may save you a
bit of time in the future).
Keep in mind, though, that it’s about four years old, so certain
sections can be out of date. Don’t worry about it too much, though. If
in doubt check out the API pages, which should always be mornin’
fresh.
thanks everybody! i do have access to the “pickaxe” book online, plus
a copy of Matz’ nutshell book, which was quite helpful to me for the
“numeric” methods in my script below, which i hereby release into the
public domain; error checking of the user input needs to be added
arabic = ‘’
while arabic.chomp != ‘q’ do
puts “Enter a whole number from 1 to 3999 for conversion”
puts “to Roman Numerals (enter ‘q’ to exit)”
arabic = gets
iArabic = arabic.to_i
roman = ‘’
rNumerals = [‘i’,‘v’,‘x’,‘l’,‘c’,‘d’,‘m’]
rIndex = 0
while iArabic > 0 do
_divmod = iArabic.divmod(10)
iArabic = _divmod[0]
digit = _divmod[1]
rPlace = ''
if digit == 4 then
rPlace = rNumerals[rIndex] + rNumerals[rIndex + 1]
elsif digit == 9 then
rPlace = rNumerals[rIndex] + rNumerals[rIndex + 2]
else
if digit > 5 then
rPlace = rNumerals[rIndex + 1]
end
rPlace += rNumerals[rIndex] * digit.modulo(5)
end
roman = rPlace + roman
rIndex += 2
BTW I always learn new language basics by figuring out how to convert
to roman numerals
arabic = ’ ’
while arabic != ‘q’ do
puts “Enter a whole number from 1 to 3999 for conversion”
puts “to Roman Numerals (enter ‘q’ to exit)”
arabic = gets
end
Here’s another way to do it
begin
puts “Enter a whole number from 1 to 3999 for conversion”
puts “to Roman Numerals (enter ‘q’ to exit)”
arabic = gets.chomp
end until arabic == ‘q’
begin
puts “Enter a whole number from 1 to 3999 for conversion”
puts “to Roman Numerals (enter ‘q’ to exit)”
arabic = gets.chomp
end until arabic == ‘q’
Although… see ruby-core 6745. Matz doesn’t like this construct, and
it may disappear.
the loop? I’ve read the following but think I will need to see an
puts “Enter a whole number from 1 to 3999 for conversion”
end until arabic == ‘q’
Although… see ruby-core 6745. Matz doesn’t like this construct, and
it may disappear.
Thanks, I wasn’t aware of that. Although I agree with him on the
inconsistency issue I don’t like the alternative approach for post test
loops either. I’d prefer something more in line with what other
languages
do - probably
begin
…
while foo > 0
do
…
while foo > 0
But then again, I can’t estimate how hard this would be for the
parser…
R> But then again, I can’t estimate how hard this would be for the
parser…
One day I’ve written a stupidity like this
svg% cat b.rb
#!./ruby
class A
def do(a, b)
puts “method A#do”
end
def toto(a = 3, d = a + 2)
self.do(1, 2)
do (a = 12)
[[‘a’, ‘b’], [‘c’, ‘d’]].each do (a ; d = a)
d += 1
p “#{a} – #{d}”
end
p “#{a} – #{d}”
end
p “#{a} – #{d}”
end
Keep in mind, though, that it’s about four years old, so certain
sections can be out of date. Don’t worry about it too much, though. If
in doubt check out the API pages, which should always be mornin’
fresh.
The new version is out now. I’ve just bought it, and I’m in the process
of using it to learn Ruby. So far, so good!
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.