3 Replies - 1072 Views - Last Post: 01 October 2013 - 05:52 PM Rate Topic: -----

#1 heaphyg  Icon User is offline

  • D.I.C Head

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

Why am I getting an error with this code?

Posted 01 October 2013 - 04:44 PM

I am doing the Ruby tutorial on codeacademy.com. All of the code except the code on the last line has been approved. For some reason I am getting an error when I try to call my
proc. The error is as follows: compared with non class/module

Thank you for your time.


ages = [23, 101, 7, 104, 11, 94, 100, 121, 101, 70, 44]

under_100 = Proc.new{ |x| x.is_a?Integer < 100}
youngsters = ages.select(&under_100)


This post has been edited by xclite: 01 October 2013 - 04:46 PM
Reason for edit:: Adding code tags.


Is This A Good Question/Topic? 0
  • +

Replies To: Why am I getting an error with this code?

#2 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 911
  • View blog
  • Posts: 3,181
  • Joined: 12-May 09

Re: Why am I getting an error with this code?

Posted 01 October 2013 - 04:47 PM

It's possible that somebody will diagnose this without the information, but it's MUCH easier to help you if you provide the error message.

Also, it looks like your proc doesn't make sense:

{ |x| x.is_a?Integer < 100}


x.is_a?Integer probably needs a space between ? and Integer.

You then compare the result of x.is_a? to 100... is_a is going to return a boolean. What exactly are you attempting to do?

What you probably want is:
{|x| x.is_a? Integer && x < 100}

This post has been edited by xclite: 01 October 2013 - 04:49 PM

Was This Post Helpful? 0
  • +
  • -

#3 heaphyg  Icon User is offline

  • D.I.C Head

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

Re: Why am I getting an error with this code?

Posted 01 October 2013 - 05:01 PM

Keep on mind that this code is being typed into the code academy website. The error message that I recieve on the website is : compared with non class/module (that's all)

Create a proc called under_100 that checks if a number it's passed is less than 100.
# at this point i submitted under_100 = Proc.new{ |x| x.is_a?Integer < 100} and it was approved.

Now we'll be using the ages array. Create a variable called youngsters and set it equal to calling .select on ages, and pass in your under_100 proc to filter for the ages that are less than one hundred. Remember to pass &under_100 to convert your proc to a block!

# at this point i submitted youngsters = ages.select(&under_100) and it was not approved.
The suggested syntax from the website is as follows: Your syntax should look like this:

youngsters = ages.select(your_proc)
Where your_proc is the under_100 proc. Don't forget the ampersand!
Was This Post Helpful? 0
  • +
  • -

#4 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 911
  • View blog
  • Posts: 3,181
  • Joined: 12-May 09

Re: Why am I getting an error with this code?

Posted 01 October 2013 - 05:52 PM

View Postheaphyg, on 01 October 2013 - 08:01 PM, said:

Keep on mind that this code is being typed into the code academy website. The error message that I recieve on the website is : compared with non class/module (that's all)

This is exactly the correct error. You're comparing Integer to 100.

Quote

Create a proc called under_100 that checks if a number it's passed is less than 100.
# at this point i submitted under_100 = Proc.new{ |x| x.is_a?Integer < 100} and it was approved.

That seems dubious. If we load up IRB and try it out, what do we get?
under_100 = Proc.new{|x| x.is_a?Integer > 100}
=> #<Proc:...@(irb):1>
under_100.call(2)
TypeError: compared with non class/module


Sound familiar? Whether codeacademy accepted it or not, the code inside your proc isn't valid.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1