0

I'm getting a NameError, which supposedly indicates that my array "pentagonals" is undefined. But I'm a monkey's uncle if I didn't define it in line 1. What am I forgetting/misunderstanding? The goal is to write a method that tells me whether a given number is pentagonal.

pentagonals = []

def pent?(num)
  pentagonals.include?(num)
end

(1..1000).each {|i|
  pentagonals << (i * (3 * i - 1) / 2)
  }

puts pent?(1)
0

2 Answers 2

2

Global variables in Ruby are distinguished from every other program name (like regular variables, class names, methods names, module names, etc.) by an initial $, so you should change your program in this way:

$pentagonals = []

def pent?(num)
  $pentagonals.include?(num)
end

(1..1000).each {|i|
  $pentagonals << (i * (3 * i - 1) / 2)
  }

puts pent?(1)

Note that global variables should be used sparingly, in fact they are dangerous because they can be written to from anywhere in your program.

5
  • I'd prefer to see an answer explaining what global variables are, why they would solve this problem, but what other better solutions there are instead. Sep 2, 2015 at 5:07
  • I'll update the answer.
    – Renzo
    Sep 2, 2015 at 5:08
  • I haven't covered the different kinds of variables, but I'm used to chucking them in methods and iterating over them without having to specify what sort of variables they are. Are arrays treated differently (for example, are they by default assigned a more localized variable tier than strings, booleans, etc?) Sep 2, 2015 at 5:27
  • Variable names are not related to their type. Arrays, strings, booleans, etc. are all objects and are all treated in the same way from this respect. What change is the way in which they are named, to denote either a different scope, or a different kind of use. For instance class attribute names start with @, class and module names start with a capital letter, etc.
    – Renzo
    Sep 2, 2015 at 5:30
  • all right, thank you for your help! Sep 2, 2015 at 5:40
0

Variables in a method are local to the method scope, unless they are passed in as parameters.

The method can also access global, class variables and other methods in the same scope.

class MyClass

  # calling methods in the same class
  def method1
    method2
  end 

  def method2
    puts 'method2 was called'
  end

  # getter / setter method pair for class variables
  # @class_variable is only accessible within this class
  def print_class_variable
    puts @class_variable
  end

  def set_class_variable(param)
    @class_variable = param
  end

  # global variables can be accessed from anywhere
  def print_global_var 
    puts $global_variable 
  end

  def self.some_class_method
    # cannot be directly accessed by the instance methods above 
  end
end

Note that global variables are not recommended as they can easily result in conflicts and ambiguity.

class Dog
  $name = "Dog" 
end

class Cat
  $name = "Cat"
end

puts $name
# which one does $name refer to? 

Not the answer you're looking for? Browse other questions tagged or ask your own question.