My classes can't use Shoes methods like para

后端 未结 1 1290
故里飘歌
故里飘歌 2021-01-24 01:58

Tldr: how do I \'include Shoes methods\' into the Array class and classes ive created, with minimal code, without totally restructuring everything?

I\'ve written a funct

1条回答
  •  清歌不尽
    2021-01-24 02:18

    I'm glad to see a question about shoes, it's been a long time. You'r new on SO so first: your question is way too elaborated, too much to read and far too little information to help you. You need to provide piecess of code that give error or don't do what you expect, things we can take over and try. This means extracting from your code tests or pieces of code that run by themself and show the problem.

    We also need to know which Ruby version and which version and color of shoes you are using. The example I'll be using is green shoes.

    I'm sure the following isn't exactly what you were after but I've made a sample based on your description of an Array that needs to be listed both by puts and para.

    Change the question or make a new one if this is not what you are after.

    require 'green_shoes'
    
    s = Struct.new(:topic, :score)
    s1 = s.new("test1", 1)
    s2 = s.new("test2", 2)
    A = [s1, s2]
    
    class Array
      def putsdata(shoes = nil)
        if shoes.class == Shoes::App
          self.each do |ea|
            shoes.para "#{ea.topic}: #{ea.score}"
          end
        else
          self.each do |ea|
            puts "#{ea.topic}: #{ea.score}"
          end
        end
      end
    end
    
    A.putsdata
    
    # gives in the console
    # test1: 1
    # test2: 2
    
    Shoes.app do
      A.putsdata(self)
    end
    
    # gives in a graphic window
    # test1: 1
    # test2: 2
    

    The puts also works in the shoes block, but of course the result doesn't come in the graphic window but on the console where you started, after the first list.

    0 讨论(0)
提交回复
热议问题