lintKid
lintKid

Reputation: 1

Ruby undefined local variable or method

i'm trying to learn ruby, i have this code.

#!/usr/bin/ruby

randomNumber = 5

class Testing
  def self.add
      puts randomNumber * 6
  end
end

Testing.add

And i get the error "Undefined local variable or method 'randomNumber'... But i have defined it .. so i thought, with the randomNumber = 5 .. i've tried int randomNumber = 5 but still no help. I have a feeling my oop sucks and i need to do more reading but i just don't get why this isn't working. Thanks for any help

Upvotes: 0

Views: 497

Answers (1)

Mulan
Mulan

Reputation: 135415

Ruby doesn't work like javascript

class Testing

  # define a class variable
  @@random_number = 5

  # "add" class method
  def self.add
    puts @@random_number * 6
  end
end

Upvotes: 2

Related Questions