/^a-eg-h/.match(‘f’) gives null in Ruby

As has been mentioned in the comments, your pattern is incorrect. It appears that you’re attempting to use a character class, but have neglected to include the surrounding square brackets. Your pattern, as it currently stands, will only match on strings that start with the literal text a-eg-h. The pattern you want is: /[^a-eg-h]/ Additionally, … Read more

Classic Hello World

This should meet the requirements that you described: class HelloWorld def self.main puts “Hello World” end end HelloWorld.main However, it’s nearly all unnecessary; you can simply use a one-line program: puts “Hello World”

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

How to remove empty string in hash

hash = {“name”=>”XYZ”, “address”=>{“street”=>{“street_address”=>””,”city”=>”City name”}}, “form”=>{“id”=>11,”f_name”=>””}, “test”=>””} def remove_blanks hash hash.map do |k, v| v == ” ? nil : [k, v.is_a?(Hash) ? remove_blanks(v) : v] end.compact.to_h end remove_blanks hash #⇒ { # “address” => { # “street” => { # “city” => “City name” # } # }, # “form” => { # “id” … Read more