call variables of another definition and continue

Asked

Viewed 38 times

0

I have some variables that repeat a lot and I would like to group them into one definition, to be called by several other definitions.

Example:

module Andre
  module Dialogos

    def self.um
      puts "estou em um"
      self.dois #chamar tudo da definição dois
      puts "#{vogais}"
      puts "estava em dois, agora estou em um."
      self.tres
    end

    def self.dois
      vogais = "estou em dois"
      consoantes = "b, c, d, e, f"
    end

    def self.tres
      puts "estou em tres"
      self.dois #chamar tudo da definição dois
      puts "#{consoantes}"
      puts "estava em dois, voltei para o três"
    end

    self.um
  end
end

In this case, an error occurs because it is not possible to know who are vowels and consonants, so the code only displays the first puts.

I would like to call the vowel variable of definition two and continue doing my calculations in setting one.

1 answer

0


If someone has this same problem, I solved by studying about the variables and discovered the Class Variable, which is defined by @@.

In this case, for my code to be functional, I did the following:

module Andre
  module Dialogos

    def self.um
      puts "estou em um"
      self.dois #chamar tudo da definição dois
      puts "#{@@vogais}"
      puts "estava em dois, agora estou em um."
      self.tres
    end

    def self.dois
      @@vogais = "estou em dois"
      @@consoantes = "b, c, d, e, f"
    end

    def self.tres
      puts "estou em tres"
      self.dois #chamar tudo da definição dois
      puts "#{@@consoantes}"
      puts "estava em dois, voltei para o três"
    end

    self.um
  end
end

Now it works!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.