Newbie scope issue


#1

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


#2

On 11/25/05, removed_email_address@domain.invalid removed_email_address@domain.invalid wrote:

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:

while arabic.chomp != ‘q’ do

Sam


#3

btw what i’ve read is:

http://www.rubycentral.com/book/tut_expressions.html#S7

so i know about this resource


#4

thanks Sam that worked!


#5

On 11/25/05, removed_email_address@domain.invalid removed_email_address@domain.invalid wrote:

thanks Sam that worked!

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.

Sam


#6

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

end

puts roman
end


#7

removed_email_address@domain.invalid wrote:

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’

Kind regards

robert

#8

Hi –

On Thu, 24 Nov 2005, Robert K. wrote:

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’

Although… see ruby-core 6745. Matz doesn’t like this construct, and
it may disappear.

David


#9

David A. Black wrote:

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…

Kind regards

robert

#10

ts wrote:

[…]
I’ll try to add ‘do … while’ to have 4 `do’ rather than only 3 :wink:

I hope it’ll work better with this <cpu-suck.rb>:

a = 1
while (a+=1) < 5
puts a
end while a > 0

#-> :-/

daz


#11

“R” == Robert K. removed_email_address@domain.invalid writes:

R> do
R> …
R> while foo > 0

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

end

A.new.toto
svg%

svg% ./b.rb
method A#do
“ab – 13”
“cd – 13”
“12 – 5”
“3 – 5”
svg%

I’ll try to add ‘do … while’ to have 4 `do’ rather than only 3 :wink:

Guy Decoux


#12

I wrote:

a = 1
while (a+=1) < 5
puts a
end while a > 0

#-> :-/

Never mind.
The problem was I wasn’t getting /any/ output …

… caught out by no STDOUT.sync=true again :frowning:

Silly code but it is correct.

daz


#13

Sam G. wrote:

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.

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!

http://pragmaticprogrammer.com/titles/ruby/index.html