user3408293
user3408293

Reputation: 1395

program using while with array values

I am trying to write a small program that goes through an array's values outputting each individual value. When it reaches 15 it stops and outputs "too big".

Why is my logic below wrong, it makes sense to me..

x = [10,11,12,13,14,15,16,17,18]

def counting
    for x[y]
    while y < 15
        puts y
    else
        puts "too big"  
end

puts counting

I'm just learning sorry if this is a really simple solution.

Upvotes: 0

Views: 56

Answers (5)

J&#246;rg W Mittag
J&#246;rg W Mittag

Reputation: 369468

Why is my logic below wrong, it makes sense to me..

Given that you program isn't even syntactically legal, it's impossible to tell what its semantics would be if it were syntactically legal, let alone why those semantics were wrong. However, here's an idiomatic Ruby solution (you will almost never use nor see while in any Ruby code, and you will certainly never see for):

puts x.take_while {|n| n < 15 }
puts 'too big'

I prefer writing in point-free style, however in this case the only way to do that is to make use of the symmetry of the condition and invert the order of operands, which will lead to awkward logic:

x.take_while(&15.method(:>))

Ao, in this case I would rather avoid point-free style, because it no longer matches the original specification literally. Instead of "take all numbers less than 15", we now have "take all numbers such that 15 is greater than those numbers", which reads awkward.

Upvotes: 0

Dogweather
Dogweather

Reputation: 16779

I think the cleanest way to do this using while would be:

def counting(x)                                                                 
  i = 0                                                                         
  while x[i] < 15                                                               
    puts x[i]                                                                   
    i += 1                                                                      
  end                                                                           
  puts 'too big'                                                                
end                                                                             

counting([10,11,12,13,14,15,16,17,18])                                          

Upvotes: 0

Saurabh
Saurabh

Reputation: 73619

If you want a one liner:

x.each {|y| y < 15 ? puts(y) : puts("too big")  ||  break  }

If you insist using while, it can be done as following:

i = 0
while i do
  x[i] < 15 ?  puts(x[i]) :  puts("too big")  ||  break
  i+=1
end

Upvotes: 0

Frank the skank
Frank the skank

Reputation: 108

It appears though you are trying to use Ruby like you would a c-style programming language. It's possible and viable, albeit not recommended.

Code Blocks

Ruby has structure known as code blocks. Code blocks are sort of like anonymous functions. You can read more about code blocks here.

x = [10,11,12,13,14,15,16,17,18]
# This is a code block.
x.each do |y| # The y between the '|'s is the parameter caught by the code block
    if y < 15
        puts y
    else
        puts "Too big."
        break # Break out of the loop
    end
end

Upvotes: 1

user229044
user229044

Reputation: 239302

That's nothing at all like Ruby syntax. You want a .each and a simple if statement:

x.each do |y|
  if y < 15
    puts y
  else
    puts "too big"
    break
  end
end

Upvotes: 2

Related Questions