[#401849] If statement — Masoud Ahmadi <lists@...>

Will anyone be able to point out what I am doing wrong.

15 messages 2012/12/02

[#401987] Trying to get "translator" to work — JD KF <lists@...>

So, basically, I'm trying to get the below code to work properly for

12 messages 2012/12/06

[#402012] Need help to select some listbox item in different listbox together — Jonathan Masato <lists@...>

Hello,

10 messages 2012/12/07

[#402045] if n belongs to set a and m belongs to set b repeat some steps, How? — "zubair a." <lists@...>

We can do so in java and similar languages like:

11 messages 2012/12/08

[#402078] Time.new(2001, 12, 3).to_i returns wrong value — Robert Buck <lists@...>

I am doing something that not many do, I am writing a database driver

9 messages 2012/12/09

[#402145] How I can create/extract a variable/hash into the current binding in Ruby? — Ramon de C Valle <rcvalle@...>

Hi,

12 messages 2012/12/12

[#402205] Wondering About Flatiron School — "Kevin Y." <lists@...>

Hi everyone!,

35 messages 2012/12/15
[#402207] Re: Wondering About Flatiron School — Chad Perrin <code@...> 2012/12/15

On Sat, Dec 15, 2012 at 11:51:08AM +0900, Kevin Y. wrote:

[#402214] Ruby quick reference arranged in ASCII sequence? — Old Grantonian <lists@...>

As a ruby beginner, I would be grateful for any links to a ruby

17 messages 2012/12/15

[#402226] print - and strip text between tags using Nokogiri — Paul Mena <lists@...>

I'm a Ruby Newbie trying to write a program to process thousands of HTML

13 messages 2012/12/15

[#402332] Perl to Ruby: regex captures to assignment. — "Derrick B." <lists@...>

Hello all,

37 messages 2012/12/19
[#402342] Re: Perl to Ruby: regex captures to assignment. — "Derrick B." <lists@...> 2012/12/20

First of all, thanks for the fast responses!

[#402352] Re: Perl to Ruby: regex captures to assignment. — Robert Klemme <shortcutter@...> 2012/12/20

On Thu, Dec 20, 2012 at 1:38 AM, Derrick B. <lists@ruby-forum.com> wrote:

[#402357] Re: Perl to Ruby: regex captures to assignment. — "Derrick B." <lists@...> 2012/12/20

Robert Klemme wrote in post #1089733:

[#402359] trying to strip characters from a line — Paul Mena <lists@...>

I'm reading a table from a MySQL database and then processing it row by

18 messages 2012/12/20

[#402394] simple division: -9 / 5 = -2 what? — "Derrick B." <lists@...>

$ irb

13 messages 2012/12/22

[#402412] POLS and string-handling — Paul Magnussen <lists@...>

Hi,

14 messages 2012/12/22

[#402460] "Open" dialog of Windows — "Damián M. González" <lists@...>

Hi guys, been researching about pop up the "open" file dialog of

11 messages 2012/12/24

[#402466] How do I install Ruby on my Ubuntu 12.10 partition. — Kaye Ng <lists@...>

I already have Ruby installed on my Windows 7 partition.

23 messages 2012/12/25

[#402510] Ruby Association Certified Ruby Programmer — Sean Westfall <lists@...>

How well respected is this certification in the industry: Ruby

27 messages 2012/12/27
[#402528] Re: Ruby Association Certified Ruby Programmer — Peter Hickman <peterhickman386@...> 2012/12/27

On 27 December 2012 01:28, Sean Westfall <lists@ruby-forum.com> wrote:

[#402519] using shebang with rvm? — Wesley Rishel <lists@...>

What would be the appropriate path to use after a shebang in the first

10 messages 2012/12/27

[#402555] numeric? — Brandon Weaver <keystonelemur@...>

I've found a bit of an annoyance trying to find out if a number is numeric

20 messages 2012/12/27

[#402580] Ruby Koans regarding Hashes. — "Derrick B." <lists@...>

I am trying to understand this, so let me know how I do. :) I know

18 messages 2012/12/28

[#402609] can't open new ruby program under "new" context menu — "Lee V." <lists@...>

I'm stuck on the new version at trying to do something very simple.

10 messages 2012/12/28
[#402618] Re: can't open new ruby program under "new" context menu — "Lee V." <lists@...> 2012/12/28

I just uninstalled what I had and reinstalled using the steps given in

[#402645] Re: can't open new ruby program under "new" context menu — "Derrick B." <lists@...> 2012/12/29

Lee V. wrote in post #1090514:

[#402653] Re: can't open new ruby program under "new" context menu — Lee Veinot <lee_veinot@...> 2012/12/30

Well, I'm up to page 43 in Chris Pine's book and having a lot of fun, but I still can't figure out two basic things.  One is what I've already asked you about.  I'm just going to paste what his book says so you can see what I'm having trouble with:

[#402642] require "test/unit" — "Mattias A." <lists@...>

Hi,

17 messages 2012/12/29
[#402667] Re: require "test/unit" — "Mattias A." <lists@...> 2012/12/31

Hi Dami叩n M. Gonz叩lez!

[#402747] Re: require "test/unit" — "Derrick B." <lists@...> 2013/01/04

Mattias A. wrote in post #1090700:

[#402749] Re: require "test/unit" — sto.mar@... 2013/01/04

Am 04.01.2013 19:48, schrieb Derrick B.:

Re: If statement

From: tamouse mailing lists <tamouse.lists@...>
Date: 2012-12-04 03:21:26 UTC
List: ruby-talk #401887
On Sun, Dec 2, 2012 at 4:43 PM, Masoud Ahmadi <lists@ruby-forum.com> wrote:
> Will anyone be able to point out what I am doing wrong.
> The code does not understand that I have said Q
>
>     loop {
>       puts("give me the command to execute or press Q to quit")
>       op=gets
>       if "#{op}" == "Q"
>         then
>         Kernel.exit(0)
>       else
>         value = `#{op}`
>         puts "#{value}"
>       end
>     }
>
> --
> Posted via http://www.ruby-forum.com/.

While others have addressed some of the syntax problems, I'd like to
address the overall approach.

In a "try something out, see how it works" approach, this is great.
But there are better ways to do something like this.

There are times when rolling a non-terminating loop with a mid-point
exit makes sense. However, there isn't much need to call the exit
method to do that.


loop do

break if condition

end

is the typical way to do this. This leaves your program a place to do
any sort of end clean up you might like to do, and so on. This sort of
thing is nice in the scenario you've devised at it most cleanly limits
the prompt, setting of the variable and test for completion once each
inside the loop, unlike while and unless will in this case.

If you'll permit, and this is in no way meant to say you're wrong in
your approach, just an offer, this is how I'd approach such a thing:

loop do
  puts("give me the command to execute or enter Q to quit")
  op = gets.strip   # the reason for strip was given above
  break if op == 'Q'
  value = system op ## HOLY KRAKEN ARE YOU SURE YOU WANT TO DO THIS???
  puts value
end

The point here is that calling the Kernel.exit(0) method makes the
code look a lot more complex that it is. Reserve the use of exit for
times when you want your code to exit early and report an error
(non-zero exit status) and then only if you've gotten yourself into a
place you don't really want to unwind, or it's just more clear to the
code reader what should be done. (Kinda like you should never end a
sentence with a preposition, but that is something with which one
aught not put up.) (Apologies, Churchill witicism.)

The other thing I'd like to point at is the if modifier -- these are
really nice little features in ruby (other languages have them, too)
that make the above very clean, to my eye. (Others will disagree, of
course.) But as you have this construction:

>       if "#{op}" == "Q"
>         then
>         Kernel.exit(0)
>       else
>         value = `#{op}`
>         puts "#{value}"
>       end

I just want to point out that the "then" is not needed at all, nor is
the else. Calling exit at that point *exits* the program immediately
-- there is no else possible. Shortening what you have a bit:

if op == "Q"
  Kernel.exit(0)
end

value = ... (and the rest)

is sufficient; Placing it in an else clause is overkill (and could
actually cause someone to overlook something if they were maintaining
such a construction).

Joel mentioned the issue of trusting users *not* to do something
malicious with thi (this DOES include yourself! -- mistakes do
happen). Something learn very early is never to trust user (or any)
input. I assume this is just a practice piece, which is fine.

Given the nature of this snippet, I'm assuming you are a beginning
programming? This is great! I hope you take my response in the manner
intended: encouraging, showing a little beyond the basic question "why
doesn't it work". Keep at it!

In This Thread