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]
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]
eval expects a string with expression representing valid Ruby code.
It can be translated to JavaScript as: function greeting_for (name) { return `Hello, ${name}!` } console.log( greeting_for( “Marty” ) )
This will take care of 0‘s in between the phone numbers phone=”08763843478,+918763843478,08763843478,+918763843478,+918763840008″ phone.split(‘,’).map{|num| num.gsub(/^\+91|^0/, ”)}.join(‘,’) #=> “8763843478,8763843478,8763843478,8763843478,8763840008”
Tracker.where(name: “Permits Required”)
To get factorial of n (1..n).inject :* To take care of zero (1..n).inject(1, :*)
your variable name should not start with Capital letter, > down = [“up 1”, “up 2”, “up 3”] > numbers_ary = down.map{|s|s[-1]} #=> [“1”, “2”, “3”] OR > numbers_ary = down.map{|s| s.chars.last} #=> [“1”, “2”, “3”] make a conditional statement like for.each based on the result If you make a loop of an array and … Read more
In Ruby you have a number of options for your PDF authoring, and your DB management is very easy if you use the ActiveRecord library (comes with Rails) or Sequel to read or write data. Complete and heavy duty PDF authoring can be done using Prawn… it requires some studying, but it’s an amazing library. … Read more
Why regular expression on numbers? You can select all numbers less than 1000 and greater than 99. my_array.select { |n| n<1000 && n>99 }
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