语法检查或“编译” Ruby on Rails 应用程序

发布于 2024-07-27 03:16:03 字数 592 浏览 6 评论 0原文

我是 Ruby 新手,最近在创建 Ruby on Rails 应用程序时遇到了与值进行比较的问题。 在控制器中,我有以下始终返回 false 的语句:

if (user.id != params[:id])

问题是 user.id (它是 Active Record)是一个整数,而 params[:id] 是一个字符串。 我花了一段时间才弄清楚这一点,最后我将其更改为:

if (user.id != params[:id].to_i)

现在该语句按预期工作。

为了避免将来出现此错误,是否有一种方法可以“编译”或让 Ruby 在您尝试比较两种不同类型时发出警告? 我遇到的一些我想“编译检查”的其他问题是:

  • 如果我创建一个变量但不使用它,则警告我。 帮助检查变量名称中的拼写错误。
  • 确保类中存在方法,这样我就可以避免方法名称拼写错误,也可以帮助重构,例如,如果我重命名一个方法。

我目前在 Windows 上使用 Ruby 1.8.6-27 RC2 以及 Rails 2.3.2 和 RadRails IDE。

I'm new to Ruby and recently ran into an issue comparing to values when creating a Ruby on Rails application. In a controller I had the following statement that always returned false:

if (user.id != params[:id])

The problem was the user.id (which is an Active Record) is an integer and params[:id] is a string. It took me a while to figure this out and I finally changed it to:

if (user.id != params[:id].to_i)

Now the statement works as expected.

To avoid this error in the future is there a way to "compile" or get Ruby to warn you if you try to compare 2 different types? Some other issues I've ran into that I would like to "compile check" are:

  • Warn me if I create a variable but don't use it. To help check for typos in variable names.
  • Make sure a method exists in a Class so I can avoid method name typos and also to help refactoring, for example if I rename a method.

I'm currently using Ruby 1.8.6-27 RC2 with Rails 2.3.2 and RadRails IDE on Windows.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

霓裳挽歌倾城醉 2024-08-03 03:16:03

先测试,再编码。 如果您编写的测试涵盖应用程序的所有分支,则可以确保您的代码运行并产生正确的结果。

编辑:我应该指出,比较两种类型的能力,直到最后一秒才依赖于方法名称,等等是 Ruby 的核心功能。

与其说您调用方法,不如说您向对象发送消息。 然后该对象负责确定如何处理该方法。 在 Rails 中,这用于访问 ActiveRecord 中的数据库列。 在将带有列名称的消息发送到对象之前,没有适用于列的方法。

Ruby 中的静态类型与鸭子类型系统背道而驰。 人们通常可以免费获得多态性,而不必担心复杂的继承/接口方案。

我建议接受这些功能并通过测试来补偿不确定性

Test first, then code. If you write tests that cover all branches of your application, you get the assurance that your code both runs and produces correct results.

EDIT: I should point out that the ability to compare two types, not depend on method names until the last second, etc. are core features of Ruby.

You don't call a method so much as you send a message to an object. The object is then responsible for figuring out how to handle the method. In Rails this is used to access DB columns in ActiveRecord. There are no methods for the columns until a message with the column name is sent to the object.

Static typing in Ruby goes against the duck typing system. One can often get polymorphism for free without worrying about complex inheritance/interface schemes.

I suggest embracing these features and compensate for the uncertainty through testing

雪落纷纷 2024-08-03 03:16:03

Ruby 不允许您重新定义 Object 的 == 运算符。 在 ruby​​ 1.8 中你不能,Ruby 1.9 应该可以,但我无法让我的脚本适用于核心类。 它适用于自定义对象。

class Object

  alias :equal_without_warning :==

  def ==(object)
    unless self.class == object.class
      warn("Comparing `#{self.class}' with `#{object.class}'")
    end
    equal_without_warning(object)
  end

end

假设我没有犯一些愚蠢的编码错误,答案是否定的:您无法检查是否正在比较不同类型的对象。

另外,我想说你不会。 实际上 Ruby 并不是设计来以这种方式工作的,这更多的是一种 java 方法而不是 Ruby 风格。

Ruby doesn't allow you to redefine the == operator for Object. In ruby 1.8 you can't, Ruby 1.9 was supposed to do but I haven't been able to get my script working for core classes. It works well for custom defined objects.

class Object

  alias :equal_without_warning :==

  def ==(object)
    unless self.class == object.class
      warn("Comparing `#{self.class}' with `#{object.class}'")
    end
    equal_without_warning(object)
  end

end

Assuming I didn't do some stupid coding error, the answer is NO: you can't check whether you are comparing different type of objects.

Also, I would say you don't. Actually Ruby isn't designed to work in this way, this is more a java approach rather than Ruby style.

清醇 2024-08-03 03:16:03

Ruby 不应该是安全的。 它可以让您比较任意两个对象,这就是它的大部分功能的来源。 如果没有这种动态设计,Rails 就不可能实现。

即使 Java 或 C 等编译语言也不会阻止您对两个对象执行 == 操作。 正如本所说,最好先进行测试。 检查您正在使用的结构。 获取有关 Ruby 对象信息的一种方法是使用:

puts object.class

Ruby isn't supposed to be safe. It lets you compare any two objects, and that's where much of its power comes from. Rails wouldn't be possible without such dynamic design.

Even a compiled language such as Java or C won't stop you from doing == on two objects. As Ben said, it's best to test first. Inspect the structures you're working with. One way to get information about a Ruby object is to use:

puts object.class
胡大本事 2024-08-03 03:16:03

一般来说,(据我所知)避免动态/脚本语言出现此类问题的最佳方法是将“逻辑”移至方法/命令并为它们编写单元测试。 基本上,任何可能失败的事情都应该进行测试。 页面上的代码应该是愚蠢的逻辑...而不是只显示那些满足特定条件的项目,它应该显示所有项目,并从仅返回应显示的项目的方法中获取该项目列表。

In general, the best way (I know of) to avoid this type of issue for dynamic/scripting languages is to move "logic" to methods/commands and write unit tests for them. Basically, anything that can fail should be tested. The code on the page should be dumb logic... rather than display only those items that meet a certain criteria, it should display all items, and get that list of items from a method that only returns the ones that should be displayed.

毁虫ゝ 2024-08-03 03:16:03

我建议两件事:

一:阅读 IRB(或 Rails 的脚本/控制台)。 动态语言中的常见开发实践是在“实时”解释器(如 IRB 或 Rails 控制台)中尝试代码片段。 这种做法可以追溯到最早的动态语言,如 Smalltalk 和 Lisp。 Ruby-debug 对于解决问题也非常有用,并且是找出示例中错误的非常简单的方法。

二:阅读“Duck Typing”。 “类型”和变量在 Ruby 中的工作方式与许多人期望的略有不同。 据我了解,像 user.id 这样的变量没有“类型”。 user.id 指向的值 确实有类型,但变量本身没有。 我相信这就是为什么没有工具可以在运行程序之前告诉您错误是什么的部分原因。 比较这两个变量不会出错,因为这些变量没有类型。 user.id 在程序中此时指向一个整数,但是将 user.id 指定为指向一个字符串是完全合法的,此时比较会更有意义。 :-)

Two things I'd suggest:

One: Read up on IRB (or script/console for rails). A common development practice in dynamic languages is to try out snippets of code inside a "live" interpreter (like IRB or the rails console). This practice goes back to the earliest dynamic languages like Smalltalk and Lisp. Ruby-debug is also really useful for troubleshooting problems and would have been a really easy way to figure out the error in your example.

Two: Read up on "Duck Typing". "Types" and variables work a little bit differently in Ruby than many folks expect them to. As I understand it, a variable like user.id doesn't have a "type". The value pointed to by user.id does have a type, but the variable itself doesn't. I believe that's part of why there's no tool that would have told you what your error was in advance of running the program. Comparing those two variables isn't an error because the variables don't have a type. user.id was pointing to an integer at that point in your program, but it would be perfectly legal to assign user.id to point to a string, at which point that comparison would have made a lot more sense. :-)

别靠近我心 2024-08-03 03:16:03

我发现的最佳解决方案是一个可以进行即时语法检查的 IDE,例如 RubyMine。 我不确定它是否能解决我原来的问题,但它帮助我找到并修复了其他几个语法和编译错误。 谢谢大家的建议。

The best solution I found was a IDE that did on-the-fly syntax checking, such as RubyMine. I'm not sure if it would have solved my original problem but it has helped me find and fix several other syntax and compile errors. Thank you everyone for your suggestions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文