0

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.

6
  • What is your question? Commented Jun 24, 2014 at 13:52
  • In your example code, you don't mention myary at all. What does the array have to do with checking the value of myvar? Commented Jun 24, 2014 at 14:00
  • Now that is I just don't know what to do with myary, so I do that ridiculous or conditional. I know I should do something with the array. but I'm to dumb to know it, that's why I need help. Commented Jun 24, 2014 at 14:57
  • @LuminaChen: As you are asking the question, you cannot be too dumb :-) However that does not spare you from deciding what you want. All of the answers solve a problem that looks remotly like yours. However, as long as we do not understand exactly what you want we have a hard time providing it. Especially the fragment "index 0 is nil" left me confused. What shall happen if myvar == nil? Or to put this the other way round: What is the actual problem you are trying to solve? Commented Jun 24, 2014 at 16:06
  • @Patru Sorry because I'm to dumb, I have said "I'm a noob" and Ihave state this too "I hope I explain it clearly because I myself is a little confused with this damn task" Therefore again I'm sorry if I'm not be clear, if myvar == nil it should not execute the code, the reason I still no got the best answer is like I have edited in my question, when myvar is a float like 1.5, 2.5 the code still executed, puts "I can do that". My goal : Can someone help me with how to check myvar value with an elegant code, if myvar has the same value with one of index(in this case index 1 - 25) in myary.. Commented Jun 24, 2014 at 16:51

3 Answers 3

3

I think what you want is this

puts "I can do that" unless myary.include?(myvar)
Sign up to request clarification or add additional context in comments.

3 Comments

That is certainly close, but it implicitly assumes that myvar and myary are related (which is not stated explicitly). It also neglects the (strange) condition that myary[0] should be nil (the OP explicitly stated this). However the task at hand should be possibly with just puts "I can do that" unless (1..25).to_a.include? myvar which does not even look at myary. Note that this does not print for myvar=24.5 which is different from the pure range solution of @Max.
Ups sorry did not notice this, It was very late of night, and maybe I'm to sleepy, sorry..
Ups sorry did not notice this, It was very late of night, and maybe I'm to sleepy, sorry..
2

You can compare myvar to a range:

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

1 Comment

I would prefer (1..25).include? myvar
0

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.

5 Comments

Hi patru apparently I'm to sleepy last night and didn't notice your answer in comment below, I'm very sorry. And also I confused with my logic, yes it should print up the code, if myvar value is a float. And if myvar nil the code should executed too. Sorry I have confused you with my silly question. But thanks you're great. Only I notice when myvar is nil it's not executed the code "I can do that". How can I make it print out "I can do that" if myvar value is nil? Thanks Patru :) Well sorry for my "great" english. That's my last problem.
@LuminaChen: As far as I can tell the first code block in this answer should do what you wanted. I have added nil as an example in the second block and it prints out the text (even if it does not show nil in parentheses, but that would be a different problem). If you have more or different requirements you should add a code block as the second one to your answer and supplement it with your expected answer. Code is very expressive and it can tell programmers a lot more than a lot of words (as most of us have sorely learnt). StackOverflow is the right place to ask your questions with code.
Umm but the code is valid because it indicates that myary contains ` 1 - 25 from index 0 - 24. And in my case index 0 is equal to nil. Actually here's the task I have from my senior, sorry not post it earlier : myary = [] myary[0] = nil for i in 1..25 ` myary[i] = 0 + i ` So that's why index 0 is explicitly equal to nil. And when I use your puts "I can do that" unless myary.include? myvar It's not print I can do that if myvar = nil Or I missed something again? Sorry for all this problem patru :(
@LuminaChen: Code looks messy in comments if it is more than a single line, that's why I suggested you edit the question. If you have to use myary as you defined it then you would probably have to use myary.index(myvar) to check an element, but that will return nil if myvar cannot be found and you would have to check for this explicitly. That will make it puts "I can do that" if myary.index(myvar).nil? || myary.index(myvar) < 1 which is ugly as it relies on nil being the first element, but it should do. However you might just as well check for nil explicitly. Strange exercise.
hello patru. puts "I can do that" if myary.index(myvar).nil? || myary.index(myvar) Yes this really work nice. Thank you very much patru. Yeah this strange exercise come from logic class, and I believe the people there also strange. Anyway thanks patru I appreciate it very much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.