生活资讯
Ruby From Other Languages
2025-01-03 00:20  浏览:118

This document contains two major sections. The first attempts to be a rapid-fire summary of what you can expect to see when going from language X to Ruby. The second section tackles the major language features and how they might compare to what you’re already familiar with.

  • To Ruby From C and C++
  • To Ruby From Java
  • To Ruby From Perl
  • To Ruby From PHP
  • To Ruby From Python

Here are some pointers and hints on major Ruby features you’ll see while learning Ruby.

Two Ruby features that are a bit unlike what you may have seen before, and which take some getting used to, are “blocks” and iterators. Instead of looping over an index (like with C, C++, or pre-1.5 Java), or looping over a list (like Perl’s , or Python’s ), with Ruby you’ll very often instead see

For more info on (and its friends , , , , etc.), see (and then ).

There’s no difference between an expression and a statement. Everything has a value, even if that value is . This is possible:

Many Ruby newbies struggle with understanding what Symbols are, and what they can be used for.

Symbols can best be described as identities. A symbol is all about who it is, not what it is. Fire up and see the difference:

The methods returns the identity of an Object. If two objects have the same , they are the same (point to the same Object in memory).

As you can see, once you have used a Symbol once, any Symbol with the same characters references the same Object in memory. For any given two Symbols that represent the same characters, the s match.

Now take a look at the String (“george”). The s don’t match. That means they’re referencing two different objects in memory. Whenever you use a new String, Ruby allocates memory for it.

If you’re in doubt whether to use a Symbol or a String, consider what’s more important: the identity of an object (i.e. a Hash key), or the contents (in the example above, “george”).

“Everything is an object” isn’t just hyperbole. Even classes and integers are objects, and you can do the same things with them as with any other object:

Constants are not really constant. If you modify an already initialized constant, it will trigger a warning, but not halt your program. That isn’t to say you should redefine constants, though.

Ruby enforces some naming conventions. If an identifier starts with a capital letter, it is a constant. If it starts with a dollar sign (), it is a global variable. If it starts with , it is an instance variable. If it starts with , it is a class variable.

Method names, however, are allowed to start with capital letters. This can lead to confusion, as the example below shows:

Now is 10, but is 11.

Like in Python, since Ruby 2.0 methods can be defined using keyword arguments:

In Ruby, everything except and is considered true. In C, Python and many other languages, 0 and possibly other values, such as empty lists, are considered false. Take a look at the following Python code (the example applies to other languages, too):

This will print “0 is false”. The equivalent Ruby:

Prints “0 is true”.

In the following Ruby code,

You might expect to be public. Not so. The access modifier continues until the end of the scope, or until another access modifier pops up, whichever comes first. By default, methods are public:

, and are really methods, so they can take parameters. If you pass a Symbol to one of them, that method’s visibility is altered.

In Java, means a method is accessible by anyone. means the class’s instances, instances of descendant classes, and instances of classes in the same package can access it, but not anyone else, and means nobody besides the class’s instances can access the method.

Ruby differs slightly. is, naturally, public. means the method(s) are accessible only when they can be called without an explicit receiver. only is allowed to be the receiver of a private method call.

is the one to be on the lookout for. A protected method can be called from a class or descendant class instances, but also with another instance as its receiver. Here is an example (adapted from The Ruby Language FAQ):

Ruby classes are open. You can open them up, add to them, and change them at any time. Even core classes, like or even , the parent of all objects. Ruby on Rails defines a bunch of methods for dealing with time on . Watch:

In Ruby, methods are allowed to end with question marks or exclamation marks. By convention, methods that answer questions end in question marks (e.g. , which returns if the receiver is empty). Potentially “dangerous” methods by convention end with exclamation marks (e.g. methods that modify or the arguments, , etc.). Not all methods that change their arguments end with exclamation marks, though. replaces the contents of an array with the contents of another array. It doesn’t make much sense to have a method like that that doesn’t modify self.

Singleton methods are per-object methods. They are only available on the Object you defined it on.

Ruby doesn’t give up if it can’t find a method that responds to a particular message. It calls the method with the name of the method it couldn’t find and the arguments. By default, raises a NameError exception, but you can redefine it to better fit your application, and many libraries do. Here is an example:

The code above just prints the details of the call, but you are free to handle the message in any way that is appropriate.

A method call is really a message to another object:

Blocks (closures, really) are heavily used by the standard library. To call a block, you can either use , or make it a by appending a special argument to the argument list, like so:

You can create blocks outside of method calls, too, by calling with a block or calling the method.

Similarly, methods are also Objects in the making:

Most operators in Ruby are just syntactic sugar (with some precedence rules) for method calls. You can, for example, override Integer’s method:

You don’t need C++’s , etc.

You can even have array-style access if you define the and methods. To define the unary + and - (think +1 and -2), you must define the and methods, respectively. The operators below are not syntactic sugar, though. They are not methods, and cannot be redefined:

In addition, , etc. are just abbreviations for , , etc. and therefore cannot be redefined.

    以上就是本篇文章【Ruby From Other Languages】的全部内容了,欢迎阅览 ! 文章地址:http://sjzytwl.xhstdz.com/xwnews/957.html 
     栏目首页      相关文章      动态      同类文章      热门文章      网站地图      返回首页 物流园资讯移动站 http://mip.xhstdz.com/ , 查看更多   
最新文章
教你彻底关闭手机自动更新,老手机不再卡顿还可以继续用几年手机系统更新怎么关闭「教你彻底关闭手机自动更新,老手机不再卡顿还可以继续用几年」
当我们购买一部新手机时,第一时间关闭系统更新是非常重要的。你们知道为什么我们经常更换手机吗?不正是因为手机变得卡顿和不流
电信固话怎么设置呼叫转移座机转接到手机怎么设置「电信固话怎么设置呼叫转移」
在现代通信中,呼叫转移功能为用户提供了极大的便利,尤其是在无法接听电话或需要临时将电话转接到其他号码时。对于电信固话用户
从“出新必换”到“多年不换”,消费者为啥不爱换手机了?手机几年换一次比较好「从“出新必换”到“多年不换”,消费者为啥不爱换手机了?」
你有多久没换手机了?不少消费者反馈,自己已经一两年或更长时间没有购买新手机或新平板电脑了。从“出新必换”到“多年不换”,
求小明正确的四位手机密码手机密码破解「求小明正确的四位手机密码」
小学生题目:小明五次输入四位数的手机密码均错误,但是每次输入的密码中都有两位数字正确,且输入的数字的位
奥尼尔谈GOAT人选:NBA仅4人够格
关于谁才是NBA的GOAT,每个人心中都会有自己的看法,毕竟大家看比赛的角度不同,对于球星成色的判断也就不同。近日,奥尼尔就谈
新就业形态下,灵活就业人员权益如何保障?
原标题:新就业形态下,灵活就业人员权益如何保障?(主题)专家:多方协同推动劳动者权益保障与企业可持续发展(副题)中国妇女
5G麒麟芯+卫星通信!华为20多款中端机已备案,继续发力高性能手机「5G麒麟芯+卫星通信!华为20多款中端机已备案,继续发力」
近日,有消息称华为将不仅在高端市场回归,还将回归中端市场,将麒麟9000s芯片下放到中端手机中。同时,华为还计划在中端手机中
买台手机好过年 没选新出的S16 Pro 却买了上代15 Pro新出的手机「买台手机好过年 没选新出的S16 Pro 却买了上代15 Pro」
购买小米12 Pro翻车后,眼看离春节就几天,还是要在过年前满足自己买过年的愿望。只考虑了OPPO和VIVO的产品后,放弃了OPPO的Reno
iPhone游戏必备神器、精准识别那个老六。雷蛇手机「iPhone游戏必备神器、精准识别那个老六。」
相信很多用iPhone玩游戏的值友们都遇到过降频的问题,尤其13PM,虽然支持120hz但是只要你敢开,十分钟内必让你的屏幕黑下来,感
割裂的杭州楼市:手握千万资金抢不到房,刚需盘降价40万无人问津
“ 杭州一季度的楼市表现,如同一面多棱镜,映照出土地市场的狂热、开发商的野心、购房者的焦虑,也暴露出城市发展的不均衡。中