LuminaChen
LuminaChen

Reputation: 135

Ruby conditional, array and iterating

Well, I have a problem in Ruby about array and iterating. I'm a noob. Let's get to the point. Here's the problem :

  1. I have an array, let's say the name is myary = []
  2. myary contains 25 integer data from index 1 - 25, those are integer 1 - 25. index 0 is nil.
  3. I have a variable, let's say the name is myvar( I don't know the value of this variable).

Then I have this code

puts "I can do that"

Now here goes the task : puts "I can do that" will be executed only if myvar's value is not 1,2,3 and so on until 25. The question is how do I check the value of myvar ? And also how to give the order to execute puts "I can do that" if the value of myvar is not between 1 and 25. I might try it like this :

puts "I can do that" if myvar != 1 || myvar != 2 || myvar != 3  # and so on until myvar != 25

But I believe that is ridiculous way, if I need to check until 100 then I will be damned. Perhaps there an elegant way to do this. And perhaps someone can help a noob like me.

I hope I explain it clearly because I myself is a little confused with this damn task. Thank you very much all.

EDIT : Ahh umm sorry for not mention it earlier I just figure out something that bothered me, I notice that if myvar is a float between 1 - 25 it should not execute the code "I can do that". Now this drive me more crazy. But thanks for every answer here, though I try that it still print out "I can do that" when the value of myvar is a float i.e 1.5, 2.5 and so on.

Upvotes: 0

Views: 701

Answers (3)

Patru
Patru

Reputation: 4551

As you do not seem to notice it in a comment I will turn it into an answer and explain some more. You could do the following:

myary = (1..25).to_a
puts "I can do that" unless myary.include? myvar

The first line will turn the range 1..25 into an array with the elements from 1 to 25 at indices 0 to 24, this avoids having nil in the array which was not helpful in the first place. I will modify the code a little to see more easily if it does what you want:

myary = (1..25).to_a
[0, 1, 2, 10, 10.5, 20, 24.5, 25, 26, nil].each do |myvar|
  puts "I can do that (#{myvar})" unless myary.include? myvar
end

This yields

I can do that (0)
I can do that (10.5)
I can do that (24.5)
I can do that (26)
I can do that ()
 => [0, 1, 2, 10, 10.5, 20, 24.5, 25, 26] 

which corresponds to my understanding of what you seem to want (even if it seems to contradict your last sentence which seems to contradict whatever you have written before :-).

Please feel free to add to your question if you should desire otherwise.

If you really do not want the line to print for a float in the range from (1..25) then an array would be just the wrong data structure and you should use the range as outlined earlier. Using the same test values as before:

[0, 1, 2, 10, 10.5, 20, 24.5, 25, 26].each do |myvar|
  puts "I can do that (#{myvar})" unless (1..25).include? myvar
end

and you end up with

I can do that (0) I can do that (26)

It is up to you which version is more to your likings.

Upvotes: 0

Max
Max

Reputation: 22325

You can compare myvar to a range:

puts "I can do that" unless (1..25) === myvar

Upvotes: 2

usha
usha

Reputation: 29349

I think what you want is this

puts "I can do that" unless myary.include?(myvar)

Upvotes: 3

Related Questions