If statement

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
}

The value returned by gets includes a trailing newline. Use
gets.chomp or gets.strip to remove it.

– Matma R.

Masoud, I don’t believe you need to interpolate the op, you can just use
op, for example:

if op == “Q”

Hope this helps,

Anna

Masoud A. wrote in post #1087554:

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}`

You are also using backticks in the last line above, and backticks are
not the same as double quotes.

The gets() method always returns a string, and interpolating a string
into a string like you are doing accomplishes nothing–you can just use
the original string directly. In
addition, assigning op to the variable value and then printing value
will give you the same result as printing op directly.

loop {
puts("Enter the command to execute or press Q to quit: ")
op = gets.chomp

if [“Q”, “q”].include?(op)
Kernel.exit(0)
else
puts op
end
}

How are you going to prevent someone from entering a command that erases
your/their whole hard drive?

Are you sure this would work?

if [“Q”, “q”].include?(op)

What about

if op.downcase == ‘q’

На 3.12.2012 г. 13:35 ч., Masoud A. написа:

This row solved my problem,
if op == “Q\n”
I was not aware of this little new line thing

Cheers everyone

if [“Q\n”, “q\n”].include?(op)

should work too…


Regards,

Ivan Cenov
OKTO-7 Co. Bulgaria
[email protected], [email protected]
mobile:+359888761080,
phone:+35972366161, fax:+_35972366262

This row solved my problem,
if op == “Q\n”
I was not aware of this little new line thing

Cheers everyone

If you do trust your user enough to let them execute system commands (I
assume this is your goal with the backticks), I think the way to input a
variable is this:

system("#{op}")

On Mon, Dec 3, 2012 at 12:35 PM, Masoud A. [email protected]
wrote:

This row solved my problem,
if op == “Q\n”
I was not aware of this little new line thing

Just a note on that one: I think this solution is inferior to doing

op.chomp!
if op == “Q”

or

if op.chomp == “Q”

Reason: on other platforms you might get a different terminating
character than \n. Also the terminator is usually not considered part
of the input. You could even go a step further and get rid of
whitespace thus allowing for more valid inputs:

op = gets.strip

Kind regards

robert

On Mon, Dec 3, 2012 at 12:39 PM, Joel P. [email protected]
wrote:

If you do trust your user enough to let them execute system commands (I
assume this is your goal with the backticks), I think the way to input a
variable is this:

system(“#{op}”)

Superfluous - this is sufficient:
system op

If you do not know whether op references a String you can do

system op.to_s

Kind regards

robert

On Sun, Dec 2, 2012 at 4:43 PM, Masoud A. [email protected]
wrote:

    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!

On 4 December 2012 13:21, tamouse mailing lists
[email protected]wrote:

(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.)

I believe you mean “. . . that is something up with which one ought not
put.”


Matthew K., B.Sc (CompSci) (Hons)
http://matthew.kerwin.net.au/
ABN: 59-013-727-651

“You’ll never find a programming language that frees
you from the burden of clarifying your ideas.” - xkcd

On Mon, Dec 3, 2012 at 9:21 PM, tamouse mailing lists
[email protected] wrote:

if op == “Q”
Kernel.exit(0)
end

Oh, crikey. This:

if op == “Q”
break
end

See? Anyone can get confused :slight_smile:

On Mon, Dec 3, 2012 at 9:30 PM, Matthew K. [email protected]
wrote:

I believe you mean “. . . that is something up with which one ought not
put.”

no doubt. :slight_smile: That was how i actually wrote it, then thought, “no
that’s not it…”

Thank you guys
As you may have understood, I have recently started learning Ruby, I am
grateful for the advise you have given me

Cheers
Masoud