Ruby 的“require"语句是在类定义内部还是外部? [英] Do Ruby 'require' statements go inside or outside the class definition?

查看:42
本文介绍了Ruby 的“require"语句是在类定义内部还是外部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Ruby 中使用类文件时,您是将requires"语句放在文件顶部还是类定义中?

When using class files in Ruby, do you put the 'requires' statements at the top of the file, or inside the class definition?

推荐答案

从技术上讲,这并不重要.require 只是一个普通的方法调用,它被调用的范围不会影响它的工作方式.放置方式的唯一区别在于,无论放置在什么位置的代码被求值时,它都会被执行.

Technically, it doesn't really matter. require is just a normal method call, and the scope it's called in doesn't affect how it works. The only difference placement makes is that it will be executed when whatever code it's placed in is evaluated.

实际上,您应该将它们放在顶部,以便人们可以一目了然地看到文件的依赖关系.这是它的传统场所.

Practically speaking, you should put them at top so people can see the file's dependencies at a glance. That's the traditional place for it.

这篇关于Ruby 的“require"语句是在类定义内部还是外部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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