2 Replies - 200 Views - Last Post: 10 December 2013 - 05:26 PM Rate Topic: -----

#1 heaphyg  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 75
  • Joined: 30-August 13

lack of understanding

Posted 10 December 2013 - 03:53 PM

here is my code:

def count_between(array, lower_bound, upper_bound)
  new_array = []
  array.each do |x|
   if x >= lower_bound && <= upper_bound
   new_array.push(x)
   end
  return(new_array.length)   
end



and here is the error which I don't understand.

array.rb:4: syntax error, unexpected <=
   if x >= lower_bound && <= upper_bound


This post has been edited by macosxnerd101: 10 December 2013 - 04:01 PM
Reason for edit:: Please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: lack of understanding

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10186
  • View blog
  • Posts: 37,608
  • Joined: 27-December 08

Re: lack of understanding

Posted 10 December 2013 - 04:02 PM

Not a Ruby guy, but you usually have to include the variable in your second condition. So your syntax should probably read:
if x >= lower_bound && x <= upper_bound


Was This Post Helpful? 3
  • +
  • -

#3 Lemur  Icon User is offline

  • Pragmatism over Dogma
  • member icon



Reputation: 1335
  • View blog
  • Posts: 3,398
  • Joined: 28-November 09

Re: lack of understanding

Posted 10 December 2013 - 05:26 PM

View Postheaphyg, on 10 December 2013 - 04:53 PM, said:

def count_between(array, lower_bound, upper_bound)
  new_array = []
  array.each do |x|
   if x >= lower_bound && <= upper_bound
   new_array.push(x)
   end
  return(new_array.length)   
end



def count_between(array, range)
  array.select { |x| range.include? x }.length
end

count_between(array, (1..5))



Look into Ruby Enumerable, ranges, and implied returns.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1