我的课程不能使用像 para 这样的 Shoes 方法 [英] My classes can't use Shoes methods like para

查看:62
本文介绍了我的课程不能使用像 para 这样的 Shoes 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Tldr:如何使用最少的代码将鞋类方法"包含"到我创建的 Array 类和类中?

我已经用 Ruby 编写了一个正常运行的程序,现在我想用它制作一个 Shoes 应用.

我遇到了手册开头描述的问题 - Shoes.app 本身就是一种块,所以 self 总是引用它,而诸如para"之类的 Shoes 方法不一定可用Ruby 中到处都是放置"的方式.但是我不够聪明,无法修复它(我只是在使用 self 并以纯 Ruby 返回,请耐心等待)

例如,我为 Array 创建了一个新方法:putdata,它循环遍历学生的测试分数数组,将每个分数自动显示为列表:

self.each 做 |ea|puts/para "#{ea.topic}: #{ea.score}"结尾

在红宝石中工作.在 Shoes 中不起作用:Array 类无权访问方法 para.我试过了:

  • 制作 Array <鞋子(真的不喜欢)
  • 在程序中的不同位置添加 stack.app do...end(没有影响)
  • 试图调用 Shoes::para 而不是 para(放屁)
  • 我尝试使用 require 'file with my all classes and methods in.rb' 而不是文件中的相同代码(报告没有类的方法)
  • 尝试在调用方法之前直接要求我的代码,以确保我的代码在 Shoes 的范围内(报告没有类的方法)
  • 制作我的自定义课程(课程和学生)<鞋子,所以它可以访问它的方法(导致运行时错误)

我已经让它发挥作用:1. 从 Array 类中删除这段代码,并使其成为浮动/泛型方法而不是 Array 方法2. 私有方法错误 --> 然后改写它,而不是一个数组方法 (array.putdata) 它是一个通用方法,它接受一个数组作为参数 (putdata(array))

但我真的,真的不想通过我的代码并像这样单独地取消组织它.

这是我的第一个 1000 行程序,有 42 个方法,我努力使其尽可能易于维护和整洁,所有内容都隐藏在类或方法中以便于维护.我从大量的、循序渐进的通用方法到许多活泼的方法都得到了它,这似乎更像是 OOP 的意图.现在我能看到的唯一方法是取消 OOP,并且没有类方法或任何东西.

我希望我可以从这个整洁、功能齐全的后端无缝地运行程序:Ruby 程序有很多if string =="SAVE", save(student); else...",所以我希望使用相同的后退代码直接弹出button.click {save(student)}".

#

是否有什么基本的东西让我无法做到这一点?我能否轻松解决 para 问题,因为我的所有类都包含显示自己数据的方法?我想在每个班级的顶部复制pasta包括'鞋子方法'"并完成.

或者我是否需要从一开始就考虑使用 GUI 进行编写?

(关于我的程序的信息:

布局是一系列页面,从侧边栏链接,使用索引与链接页面直接复制自《Nobody Knows Shoes》或课堂手册样本.

学生可以输入他们的新级别,并查看他们当前进度的读数.

程序的主要部分"有通用方法,它们具有页面标题和一些说明等内容,然后调用学生对象或模块方法来按照用户的指示执行操作.

更上层:student 是一个自定义类,具有诸如保存"、显示扁平化数据"、如果此 ELSE 不这样做,则将其添加到您的级别"之类的方法,以及相关的数据位,例如包含所有内容的数组他们的课程对象在.

每门课程也是一个自定义类(模块"),其中有分数、模块名称等作为变量,还有一些小方法,如显示格式化名称,或在该模块中添加一个.)

解决方案

很高兴看到一个关于鞋子的问题,已经很久了.首先,您是 SO 新手:您的问题太详细了,阅读太多,信息太少,无法帮助您.您需要提供一些会出错或不符合您预期的代码,我们可以接手并尝试.这意味着从您的代码测试或自行运行的代码片段中提取并显示问题.

我们还需要知道您使用的是哪个 Ruby 版本以及哪个版本和颜色的鞋子.我将使用的示例是绿鞋.

我确定以下内容并不是您所追求的,但我已经根据您对需要按 puts 和 para 列出的数组的描述制作了一个示例.

如果这不是您想要的,请更改问题或创建一个新问题.

需要'green_shoes's = Struct.new(:topic, :score)s1 = s.new("test1", 1)s2 = s.new("test2", 2)A = [s1, s2]类数组def putsdata(鞋子=零)if shoes.class == Shoes::Appself.each 做 |ea|shoes.para "#{ea.topic}: #{ea.score}"结尾别的self.each 做 |ea|puts "#{ea.topic}: #{ea.score}"结尾结尾结尾结尾A.putsdata# 在控制台中给出# 测试 1:1# 测试 2:2Shoes.app 做A.putsdata(self)结尾# 在图形窗口中给出# 测试 1:1# 测试 2:2

puts 也适用于 shoes 块,但结果当然不会出现在图形窗口中,而是出现在您开始的控制台上,在第一个列表之后.

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 functioning program in Ruby, and now I want to make a Shoes app from it.

I'm having the problem described at the beginning of the manual - Shoes.app is a sort of block in itself, so self always refers to it, and Shoes methods like "para" aren't necessarily going to be available everywhere the way "puts" is in Ruby. But I'm not smart enough to fix it (I've only just got my head around using self and return in pure Ruby, bear with me)

For example, I've created a new method for Array: putdata, which loops through a student's test score array displaying each automatically as a list:

self.each do |ea|
    puts/para "#{ea.topic}: #{ea.score}"
end

Works in ruby. Doesn't work in Shoes: Array class doesn't have access to the method para. I've tried:

  • making Array < Shoes (it really doesn't like that)
  • adding stack.app do...end at various places in the program (no impact)
  • trying to call Shoes::para instead of para (farts)
  • I've tried using require 'file with all my classes and methods in.rb' instead of the same code in the file (reports no method for class)
  • tried requiring my code directly before calling a method, to ensure my code is in the scope of Shoes (reports no method for class)
  • making my custom classes (Course and Student) < Shoes, so it'd have access to its methods (causes a runtime error)

I've got it to function by: 1. Removing this bit of code from the Array class, and making it a floating/generic method rather than an Array method 2. Private method error --> then rephrasing it so instead of an Array method (array.putdata) it's a generic method which takes an array as an argument (putdata(array))

But I really, really don't want to go through my code and individually un-organise it like this.

It's my first 1000 line program, with 42 methods, and I worked hard on making it as maintainable and neat as possible, with everything tucked away in classes or methods for easy upkeep. I got it from everything in massive, step-by-step generic methods down to lots of snappy ones, which seemed a bit more like how OOP is meant to go. Now the only way I can see to make this work is to UN-OOP it, and have no class methods or anything.

I was hoping I could Shoes the program fairly seamlessly from this tidy, functional back-end: the Ruby program has lots of "if string == "SAVE", save(student); else...", so I was hoping to straightforwardly pop in "button.click {save(student)}" with the same back code.

#

Is there something fundamental I'm missing to let me do this? And can I fix the para problem easily, seeing as all my classes contain ways of displaying their own data? I'd like to copypasta "include 'Shoes methods'" at the top of each class and be done.

Or do I need to be writing with the GUI in mind from the start in future?

(Info about my program:

Layout is a series of pages, linked from a sidebar, using index with linked pages as copied straight from the Nobody Knows Shoes book, or the class-book sample.

Students can input their new levels, and view a readout of their current progress.

There are generic methods for major "parts" of the program, which have things like the title of the page and some instructions, and which then call on student objects or Module methods to do the things as instructed by the user.

Higher up: student is a custom class, with methods like "save", "display flattened data ", "add one to your level IF this ELSE don't", and associated bits of data, like an array with all their course objects in.

Each course is also a custom class ("Module"), which has the score, the module name etc as variables, and some tiny methods like display formatted name, or add one to this module.)

解决方案

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.

这篇关于我的课程不能使用像 para 这样的 Shoes 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆