0

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.

2
  • well, it's full of simple syntax errors for one. Commented Apr 24, 2014 at 3:48
  • You know, I answered because this was the shortest answer of mine so far, but in fact, puts iterates over arrays by itself. You don't have to do x.each { |e| puts e }, puts x alone will do. Try it. Commented Apr 24, 2014 at 3:53

5 Answers 5

2

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
Sign up to request clarification or add additional context in comments.

1 Comment

I am familiar with each but is there a way to do it using "while" I am trying to understand how to use while
1

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

1 Comment

I am familiar with each but is there a way of doing it using "while" I am trying to understand how to use it by doing a few examples
0

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

2 Comments

Is there a way of doing it using "while" rather than each? I am practicing using while right now
*Note for future beginners that run into this questions. In addition to your below answer I had to include the original array x = [10,11,12,13,14,15,16,17,18] for it to work
0

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])                                          

Comments

0

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.

Comments

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.