Как объявить метод и вызвать
def move(destination) puts "Running to the #{destination}" end move(test)
команда return приводит к выходу из метода, не дожидаясь его завершения
Как объявить классы и вызвать их
class Dog def talk puts "Bark!" end def move(destination) puts "Running to the #{destination}" end end class Bird def talk puts "Chip! chip!" end def move (destination) puts "Fly ti the #{destination}" end end bird=Bird.new fido = Dog.new rex = Dog.new fido.talk rex.move("food bow1") bird.talk bird.move("test") rex.talk
Bark! Running to the food bow1 Chip! chip! Fly ti the test Bark!
Переменные внутри метода существуют только до того как не будет запущен end , чтобы сохранить переменную, надо использовать @ перед переменной
@name = "sandy"
0 Comment