The problem is that although you have instance variables, you have not made them available. attr_reader :variable_name will allow you to read them, attr_writer :variable_name will allow you to write them, and attr_accessor :variable_name will allow you to do both. These are metaprogramming shortcuts built into the Ruby standard library, so you donβt need to write methods to read or write variables yourself. They take the character, which is the name of the instance variable.
class Packet attr_reader :name, :age, :number, :array def initialize(name, age, number, array) @name = name @age = age @number = number @neighbors = array end end p1 = Packet.new("n1", 5, 2, [1,2,3,4]) puts p1.name
source share