简体   繁体   English

如何在鞋中使用课程?

[英]How to use classes in Shoes?

I'm a somewhat beginner programmer who has a background in using Processing. 我是一个有点初学者的程序员,有使用Processing的背景。 I'm currently trying to make an app with Shoes, but I'm confused over how objects and classes work. 我目前正在尝试使用Shoes创建一个应用程序,但我对于对象和类如何工作感到困惑。

I understand that the following would run in Ruby: 我知道以下内容将在Ruby中运行:

class Post
    def self.print_author
      puts "The author of all posts is Jimmy"
    end
end

Post.print_author

But why won't the following run in Shoes? 但为什么以下不会在鞋子中运行? How would I make it run? 我怎么让它运行?

class Post
    def self.print_author
      para "The author of all posts is Jimmy"
    end
end

Shoes.app do
    Post.print_author
end

I'm not too familiar with Shoes, but the problem you're likely having here is that you're trying to call a method called para on the Post class, and no such method exists. 我对Shoes并不太熟悉,但你可能遇到的问题是你试图在Post类上调用一个名为para的方法,并且不存在这样的方法。

When you call Shoes.app do ... , I suspect Shoes is changing the current execution context to one that includes those methods. 当你调用Shoes.app do ... ,我怀疑Shoes正在将当前执行上下文更改为包含这些方法的上下文。 That is, you should expect this to work: 也就是说,你应该期望这个工作:

Shoes.app do
  para "The author of all posts is Jimmy"
end

This is equivalent to: 这相当于:

Shoes.app do
  self.para("The author of all posts is Jimmy")
end

When you call Post.print_author , self is no longer the Shoes object, but rather, is the Post class. 当你调用Post.print_authorself不再是Shoes对象,而是Post类。 You have a few options at that point: 那时你有几个选择:

  1. Pass in the Shoes instance, and call your Shoes-specific methods on that. 传入Shoes实例,并在其上调用特定于Shoes的方法。 You should probably do it this way when you don't need any state from Post: 当你不需要Post中的任何状态时,你应该这样做:

     class Post def self.print_author(shoes) shoes.para "The author of all posts is Jimmy" end end Shoes.app do Post.print_author(self) end 
  2. Create a Post class which accepts a Shoes object, so you don't have to keep passing it around. 创建一个接受Shoes对象的Post类,这样您就不必继续传递它。 You should do it this way if Post is going to have any substantial amount of state: 如果Post有大量的状态,你应该这样做:

     class Post def initialize(shoes) @shoes = shoes end def print_author @shoes.para "The author of all posts is Jimmy" end end Shoes.app do post = Post.new(self) post.print_author end 
  3. You could use a variant on the 2. option to automatically pass calls to the @shoes object. 您可以在2.选项上使用变量来自动将调用传递给@shoes对象。 This starts to get into Ruby metaprogramming, which I'd recommend you avoid until you're more comfortable in Ruby, but I'm leaving it here to pique your interest: 这开始进入Ruby元编程,我建议你避免,直到你对Ruby更加舒服,但我将它留在这里激起你的兴趣:

     class Post def initialize(shoes) @shoes = shoes end def print_author para "The author of all posts is Jimmy" end def method_missing(method, *args, &block) @shoes.send(method, *args, &block) end end Shoes.app do post = Post.new(self) post.print_author end 

What this does is tell Ruby "if a method isn't found on the Post instance, try sending it to the @shoes instance instead". 这样做是告诉Ruby“如果在Post实例上找不到方法,请尝试将其发送到@shoes实例”。 As you can imagine, this can allow for some very nice DSLs, but you have to use it carefully, as it can make code difficult to follow if you abuse it. 您可以想象,这可以允许一些非常好的DSL,但您必须小心使用它,因为如果您滥用它可能会使代码难以遵循。

A much simpler way to do this is to have Post provide the content, and then, in your Shoes app, render that content however you want it. 一种更简单的方法是让Post提供内容,然后在您的Shoes应用程序中,根据需要呈现该内容。 Side benefit: you can re-use your Post class in another class that prints to the console. 附带好处:您可以在另一个打印到控制台的类中重用Post类。

class Post
  def self.print_author
    "The author of all posts is Jimmy"
  end
end

Shoes.app do
  para Post.print_author
end

class ConsoleApp
  def run
    puts Post.print_author
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM