Ad
  • Custom User Avatar

    when you do

    self.class.send(:attr_reader
    

    you apply attr_reader to all objects of that class, but you need that attributes only in one object
    some useful docs are here https://ruby-doc.org/ (look for Core API and there for Class, Module, Object, Basic_Object articles)

  • Custom User Avatar

    Hello everyone,

    It would be great to get some feedback on this Error Message.

    Make sure dynamic attributes don't persist across different instances of GenericEntity objects.

    I do not understand what they mean by this. Because when I create different object they do are present because I put all the code running at initialization.

    My code so far is:

    class GenericEntity
      def initialize(hash)
        define_instance_variables_and_reader(hash)
      end
    
      def define_instance_variables_and_reader(hash)
        hash.each do |k, v|
          instance_variable_set("@#{k}", v)
          self.class.send(:attr_reader, k)
        end
      end
    end
    

    Additionally do you have any ressources to read about this topic and realted to Ruby?

    Many thanks

    Faouzi

  • Custom User Avatar

    And reverse logic confirms when I checked the attribute against the instance variable in my sample tests:

    dog = GenericEntity.new(:barks=>"obnoxiously", :bites=>"mildly", :color=>"black")
    
    Test.assert_equals(dog.color, "black")
    Test.assert_equals(dog.bites, "mildly")
    Test.assert_equals(dog.barks, "obnoxiously")
    Test.assert_equals(dog.barks, @barks)
    
    
    
    
    Test Passed: Value == "black"
    Test Passed: Value == "mildly"
    Test Passed: Value == "obnoxiously"
    Expected: nil, instead got: "obnoxiously"
    
  • Custom User Avatar

    Are you 100% sure @barks is available as an instance variable of dog and not just an attribute?

  • Custom User Avatar

    Any resolution? Yeah when I write my own sample test with same params: dog = GenericEntity.new(:barks=>"obnoxiously", :bites=>"mildly", :color=>"black")

  • Custom User Avatar

    After submitted I got such error message:

    dog's instance variable @barks isn't what it was set to in the hash - Expected: "obnoxiously", instead got: nil
    

    But I have tried to test all the tests in my local machine, I got the expected results instead of nil.

  • Custom User Avatar

    I think an example (like you used) that demonstrates the potential issue is a good way to do it. It's marginally more difficult but maybe not enough to change the ranking.

    If you wanted to make it more difficult you could come up with some story why you need to reimplement OpenStruct to have read-only attributes, but as it stands it's a good way to show the unintended side effects of metaprogramming!

  • Custom User Avatar

    I really like the idea of making sure attributes don't bleed across instances. I just need to figure out exactly how to word it in the instructions. Also, I think this may increase the difficulty...what do you think the ranking should be with this new test added?

    Update: I have added the test, but left the ranking as is for now.

  • Custom User Avatar

    How about adding this to the end of your tests:

    Test.expect(!dog.respond_to?(:engine))
    
  • Custom User Avatar

    I think the description leads you to believe that you're looking for words (as in separated by whitespace) and not just substrings.

    :alpha strings contain the word "alpha"