Ruby 术语问题:这是同时进行的 Ruby 声明、定义和赋值吗?

发布于 2024-07-15 01:22:18 字数 238 浏览 6 评论 0原文

如果我说:

x = "abc"

这看起来像是同时进行的声明、定义和赋值,无论我之前是否在程序中说过有关 x 的任何内容。

它是否正确?

我不确定 Ruby 中声明、定义和赋值的正确术语是什么,或者由于 Ruby 中的动态类型,这些东西之间是否存在区别。

@tg:关于你的观点#2:即使x在x =“abc”语句之前存在,你不能将x =“abc”语句称为定义/重新定义吗?

If I say:

x = "abc"

this seems like a declaration, definition and assignment, all at the same time, regardless of whether I have said anything about x in the program before.

Is this correct?

I'm not sure what the correct terminology is in Ruby for declarations, definitions and assigments or if there is even a distinction between these things because of the dynamic typing in Ruby.

@tg: Regarding your point # 2: even if x existed before the x = "abc" statement, couldn't you call the x = "abc" statement a definition/re-definition?

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

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

发布评论

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

评论(2

如若梦似彩虹 2024-07-22 01:22:18

差不多了。 如果在下一行,您这样做:

x = 1

那么您就重新定义了它,并分配了它(现在它是一个整数,而不是字符串)。 鸭子打字与您可能习惯的非常不同。

Pretty much. And if, on the very next line, you do:

x = 1

Then you've just re-defined it, as well as assigned it (its now an integer, not a string). Duck typing is very different to what you're probably used to.

那请放手 2024-07-22 01:22:18
  1. 声明: 否。
    在 Ruby 中谈论声明变量是没有意义的,因为没有任何东西可以与语言中的声明类似。 为编译器设计的语言具有声明,因为编译器需要提前知道数据类型有多大以及如何访问它们的不同部分。 例如,如果我用 C 说:

    int *i; 
      

    然后编译器知道某处为 i 留出了一些内存,并且它的大小足以容纳指向 int 的指针。 最终,链接器会将所有对 i 的引用挂钩在一起,但至少编译器知道它在某个地方。

  2. 定义:可能
    定义通常为某些东西设置一个初始值(至少在熟悉的编译语言中)。 如果 xx = "abc" 语句之前不存在,那么我猜你可以将其称为定义,因为此时 Ruby 必须为符号x

    不过,定义是一个特定术语,人们通常用它来区分某个变量的初始静态赋值与该变量的声明。 在 Ruby 中,没有这种声明。 如果某个变量已在当前范围内的某处被赋值,您通常会说它是已定义,如果没有,则您会说它未定义

    您通常不会谈论它的定义,因为在 Ruby 中这只是赋值。 没有特殊的上下文可以证明您可以像其他语言一样使用定义

    这让我们......

  3. 作业: 是的。
    您绝对可以将其称为赋值,因为它正在为符号x 赋值。 我认为没有人会不同意这一点。

  1. Declaration: No.
    It doesn't make sense to talk about declaring variables in Ruby, because there's nothing analogous to a declaration in the languages. Languages designed for compilers have declarations because the compiler needs to know in advance how big datatypes are and how to access different parts of them. e.g., if I say in C:

    int *i;
    

    then the compiler knows that somewhere there is some memory set aside for i, and it's as big as it needs to be to hold a pointer to an int. Eventually the linker will hook all the references to i together, but at least the compiler knows it's out there somewhere.

  2. Definition: Probably.
    A definition typically set an initial value for something (at least in the familiar compiled languages). If x didn't exist before the x = "abc" statement, then I guess you could call this a definition, since that is when Ruby has to assign a value to the symbol x.

    Again, though, definition is a specific term that people typically use to distinguish the initial, static assignment of a value to some variable from that variable's declaration. In Ruby, you don't have that kind of statement. You typically just say a variable is defined if it's been assigned a value somewhere in your current scope, and you say it's undefined if it hasn't.

    You usually don't talk about it having a definition, because in Ruby that just amounts to assignment. There's no special context that would justify you saying definition like there is in other languages.

    Which brings us to...

  3. Assignment: Yes.
    You can definitely call this an assignment, since it is assigning a value to the symbol x. I don't think anyone will disagree with that.

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