Last Updated
Viewed 09 Times
           

I have a few questions regarding the following code from Codewar coding challenge and solution. I have listed my questions under the coding solution. Many thanks in advance!

Coding challenge question is below.

An allergy test produces a single numeric score which contains the information about all the allergies the person has (that they were tested for). The list of items (and their value) that were tested are: eggs (1), peanuts (2), shellfish (4), strawberries (8), tomatoes (16), chocolate (32), pollen (64) & cats (128). So if Tom is allergic to peanuts and chocolate, he gets a score of 34. Write a program that, given a person’s score can tell them: a. whether or not they’re allergic to a given item b. the full list of allergies.

You will be provided with a class Allergies which will have 2 methods

 1. is_allergic_to Checks if Tom is allergic to a particular allergen.
    Returns True if Tom is allergic, False otherwise allergies
 2. Returns a list of what Tom is allergic to. This list must be sorted
    alphabetically
 3. Must Dos: Ensure that your function throws a TypeError for invalid
    inputs such as None(Null), floats, strings, or any data type that is
    not an integer.

The following is the Ruby code for the above challenge.

class Allergies
  ALLERGY_SCORES = {
    "eggs"=> 1,
    "peanuts"=> 2,
    "shellfish"=> 4,
    "strawberries"=> 8,
    "tomatoes"=> 16,
    "chocolate"=> 32,
    "pollen"=> 64,
    "cats"=> 128
  }

  def initialize(score)
    @score = score
  end

  def is_allergic_to(allergen)
    @score & ALLERGY_SCORES[allergen] > 0 

      begin
        @score = [/\D/]
      rescue StandardError => e
        puts "Error occured: #{e}."
        puts "Please enter integer ONLY."
      end
  end

  def allergies()
    ALLERGY_SCORES.keys.select do |allergen|
      is_allergic_to(allergen)
    end 
  end

  def acceptable_score()
    max_score = 0
    ALLERGY_SCORES.each do |allergen, score|
        max_score += score
    return max_score
  end
end

puts "Please enter allergy score or  "
score = gets.chomp 
Allergies.new.initialize(score)
Allergies.is_allergic_to
# Allergies.allergies

Question - 1 - Hash - & operator use

In the below method, allergen is referred to get the key value from the ALLERGY_SCORES allergy constant hash. Why @score & ALLERGY_SCORES[allergen] > 0? In ALLERGY_SCORES hash, only values are integers. Why is & operator used in there?

Setting both @socre and ALLERGY_SCORES to greater than 0 - is it also for validation purpose? (in case users enter negative or other values)

def is_allergic_to(allergen)
 @score & ALLERGY_SCORES[allergen] > 0 
end

Question - 2 - Error Handling

In the code, there was no error handling component. Therefore I have attempted to address this and have added the following code inside is_allergic_to method. However, I get an error.

  def is_allergic_to(allergen)
    @score & ALLERGY_SCORES[allergen] > 0 

      **begin
        @score = [/\D/]
      rescue StandardError => e
        puts "Error occured: #{e}."
        puts "Please enter integer ONLY."
      end**
  end

Question - 3 - Creating a new array of key (allergen)

.keys method is to create a new array which is the list of allergen. .select method is also used to to create a new array.

Why are both .keys and .select method used for creating a new array of key from ALLERGY_SCORES hash?

  def allergies()
    ALLERGY_SCORES.keys.select do |allergen|
      is_allergic_to(allergen)
    end 
  end

Question - 4 - Code Display Output to the Screen in Terminal

I want the user to be able to enter their allergy score. But the following code does not work.

puts "Please enter allergy score."
score = gets.chomp 
Allergies.new.initialize(score)
Allergies.is_allergic_to
Allergies.allergies

Question - 5 - maximum acceptable score validation

Maximum acceptable score is 255. How should I verify the score to make sure the score they have entered is <= 255? How should I modify this part of code?

def acceptable_score()
    max_score = 0
    ALLERGY_SCORES.each do |allergen, score|
        max_score += score
    return max_score
  end

Similar Question 1 : Ruby - modifying classes

I wrote simple method which checks if a number is Armstrong number. But now I want to modify default Number class putting method. So, I have code:

def is_an(number)
    (number.to_s.split(//).map(&:to_i).inject{|x,y|x+y**(number.size-1)}) == number ? true : false
end
p is_an(153)

I want to use it as method: 153.is_a? So, how I do this?

class Number
   def is_an
      ??? How to use object data over here? ???
   end
end

Thx a lot for reading.

Similar Question 2 : Ruby add method to a class

Assume I have a class :

class Foo
end

To add a method to this class I know 2 options:

  1. Reopening the class and implement the method :

    class Foo
      def bar
      end
    end
    
  2. using class_eval to implement method :

    Foo.class_eval { def bar; end}
    

What is the difference? Which one is better?

Similar Question 3 : Find certain object in Ruby

Always working with Rails but now that I am coding completely without framework I am missing a lot of methods that I am not able to write myself. So I need a little hint here.

So I've got this code

class Foo
   initialize(k)
    (1..k).each do |b|
       Boo.new(b)
    end
end

class Boo
  initialize(a)
    @a = a
  end
end

Foo.new(10)
#and now I want to access one of the Boo's with a certain a (in Rails Boo.find_by_a(2) e.g.)

How do I write a method that uses a certain value "a" to get the specific Boo back with @a = a?

Update:

Okay I added this methot to Boo

  def self.all
    ObjectSpace.each_object(self).to_a
  end

And now I get the right object by:

boo = Boo.all.select{ |b| b.a == 2 }.first

But if I want this to be a method, where would i put it?

Similar Question 4 (1 solutions) : Classwide method in ruby gem

Similar Question 5 (1 solutions) : Ruby method variable declaration

Similar Question 6 (3 solutions) : Ruby Instance Methods and Variables

Similar Question 7 (1 solutions) : Method undefined error - Ruby

Similar Question 8 (2 solutions) : Ruby Class Method Setup

Similar Question 9 (1 solutions) : Ruby Script Error

cc