School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,420 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,533 people online right now. Registration is fast and FREE... Join Now!




Strange ruby multiplication error

 

Strange ruby multiplication error, undefined method "*" for nil:NilClass (NoMethodError)

timothybrown

2 May, 2009 - 02:26 PM
Post #1

New D.I.C Head
Group Icon

Joined: 8 Apr, 2009
Posts: 46


Dream Kudos: 25
My Contributions
Basically, I'm getting a NoMethodError for the multiplication operator (*) on line 8.

This is a friend of mine's program, I didn't design it.

CODE

# Coefficent is designed to be an array
class Stuff
  def evaluate(coefficent, x)
    b = 0
    degree = (coefficent.length) - 1
    value = 0
    while b != (coefficent.length + 1)
      value += (coefficent[b] * x**degree)
      b = b + 1
      degree = degree - 1
    end
    return value
  end
end

#-----
$stuff = Stuff.new
#-----

class Program
  def main
    a = [1,2,3]
    p $stuff.evaluate(a,17)
  end
end

#-----
$pub_main = Program.new
#-----
$pub_main.main
#-----


User is offlineProfile CardPM
+Quote Post


vikhyat

RE: Strange Ruby Multiplication Error

2 May, 2009 - 11:03 PM
Post #2

New D.I.C Head
*

Joined: 14 Feb, 2009
Posts: 5



Thanked: 1 times
My Contributions
On line 8, coeffecient[b] is probably a nil. Try putting- "p coeffecient[b]" just before the line that is causing the problem.
User is offlineProfile CardPM
+Quote Post

JackOfAllTrades

RE: Strange Ruby Multiplication Error

3 May, 2009 - 02:11 PM
Post #3

I exist to Google your problems.
Group Icon

Joined: 23 Aug, 2008
Posts: 4,948



Thanked: 424 times
Dream Kudos: 50
Expert In: Being annoyed with lazy people.

My Contributions
Are you sure you want
ruby
while b != (coefficent.length + 1)
???

A more Ruby way:
ruby
class Stuff
def evaluate(coefficient, x)
degree = coefficient.length - 1
value = 0
coefficient.each { |c|
value += (c * x**degree)
degree = degree - 1
}
return value
end
end

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 12:09AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month