Forum: Ruby newbie scope issue

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
77e0fbde6c1c41bb57c4035f19cf9f4e?d=identicon&s=25 jemptymethod (Guest)
on 2005-11-24 14:25
(Received via mailing list)
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
91ac0ea6ae5c8cd67e2ed455b43ffbee?d=identicon&s=25 sgentle (Guest)
on 2005-11-24 14:33
(Received via mailing list)
On 11/25/05, jemptymethod@gmail.com <jemptymethod@gmail.com> 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
77e0fbde6c1c41bb57c4035f19cf9f4e?d=identicon&s=25 jemptymethod (Guest)
on 2005-11-24 14:33
(Received via mailing list)
btw what i've read is:

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

so i know about this resource
77e0fbde6c1c41bb57c4035f19cf9f4e?d=identicon&s=25 jemptymethod (Guest)
on 2005-11-24 14:49
(Received via mailing list)
thanks Sam that worked!
5befe95e6648daec3dd5728cd36602d0?d=identicon&s=25 bob.news (Guest)
on 2005-11-24 15:01
(Received via mailing list)
jemptymethod@gmail.com 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
91ac0ea6ae5c8cd67e2ed455b43ffbee?d=identicon&s=25 sgentle (Guest)
on 2005-11-24 15:13
(Received via mailing list)
On 11/25/05, jemptymethod@gmail.com <jemptymethod@gmail.com> 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
77e0fbde6c1c41bb57c4035f19cf9f4e?d=identicon&s=25 jemptymethod (Guest)
on 2005-11-24 15:33
(Received via mailing list)
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
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 dblack (Guest)
on 2005-11-24 15:42
(Received via mailing list)
Hi --

On Thu, 24 Nov 2005, Robert Klemme 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
5befe95e6648daec3dd5728cd36602d0?d=identicon&s=25 bob.news (Guest)
on 2005-11-24 15:54
(Received via mailing list)
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
956f185be9eac1760a2a54e287c4c844?d=identicon&s=25 decoux (Guest)
on 2005-11-24 16:26
(Received via mailing list)
>>>>> "R" == Robert Klemme <bob.news@gmx.net> 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 ;-)



Guy Decoux
Dce0999389d102f9a313af625375304c?d=identicon&s=25 dooby (Guest)
on 2005-11-27 06:08
(Received via mailing list)
ts wrote:
>
> [...]
>  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
Dce0999389d102f9a313af625375304c?d=identicon&s=25 dooby (Guest)
on 2005-11-27 06:44
(Received via mailing list)
I wrote:
>
> 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
149379873fe2cb70e550c6bff8fedd0c?d=identicon&s=25 jeff (Guest)
on 2005-11-27 19:13
(Received via mailing list)
Sam Gentle 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
This topic is locked and can not be replied to.