newbie scope issue

J

jemptymethod

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
 
S

Sam Gentle

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 !=3D 'q' do

Sam
 
R

Robert Klemme

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

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
 
S

Sam Gentle

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
 
J

jemptymethod

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
 
D

David A. Black

Hi --

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
 
R

Robert Klemme

David said:
Hi --



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
 
T

ts

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 ;-)



Guy Decoux
 
D

daz

ts said:
[...]
I'll try to add 'do ... while' to have 4 `do' rather than only 3 ;-)

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

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

#-> <yawn> :-/


daz
 
D

daz

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

#-> <yawn> :-/

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

.... caught out by no STDOUT.sync=true again :(

Silly code but it is correct.


daz
 
J

Jeffrey Schwab

Sam said:
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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,310
Members
47,977
Latest member
MillaDowdy

Latest Threads

Top