Armstrong numbers in ruby

As already suggested you can utilise digits. Used with reduce you can write something like this: number.select { |n| n.digits.reduce(0) { |m, n| m + n**3 } == n } #=> [153, 370]

how to i add objects to an array ? ruby

I think the following will be of some help to get started: class Dog def speak puts “woof” end end class Cat def speak puts “meow” end end class PetLover attr_accessor :species def initialize @species = [Dog, Cat] end def random_animal @species[rand(@species.size)].new end def animals(n) ary = [] n.times do ary << random_animal end ary … Read more