ruby 新建对象_Ruby中的对象

ruby 新建对象

Ruby对象 (Ruby objects)

We are well aware of the fact that Ruby is an object-oriented scripted programming language. Thus, everything we do in Ruby is directly related to objects. Ruby program can also be considered as a stream of tokens. The tokens are nothing but various Ruby keywords, operators, and a variety of literals. Objects stand as the main part of the Ruby program if the semantic view is contemplated.

我们很清楚Ruby是一种面向对象的脚本编程语言。 因此,我们在Ruby中所做的一切都与object直接相关。 Ruby程序也可以视为令牌流。 令牌不过是各种Ruby关键字,运算符和各种文字而已。 如果考虑语义视图,则对象将成为Ruby程序的主要部分。

Objects are created, manipulated and destroyed during the execution of a Ruby program.

在Ruby程序执行期间创建,操作和销毁对象

We have got two types of objects in Ruby:

Ruby中有两种类型的对象

  1. Built-in objects

    内建物件

    Ruby has a rich set of libraries for increasing the efficiency of programming. Built-in objects come from those pre-defined libraries. They are available to the application programmers and they can use it as per the requirement of their code.

    Ruby具有丰富的库集,可提高编程效率。 内置对象来自那些预定义的库。 应用程序程序员可以使用它们,并且可以根据其代码要求使用它们。

  2. Custom objects

    自定义对象

    Custom objects are created by the application programmers according to the demand of their program before their usage in the code.

    自定义对象由应用程序程序员根据其程序需求在代码中使用之前创建。

Object creation can also be called as Object instantiation. An object consisted of two things: the first one is called Data and later one known as Method or Function.

对象创建也可以称为对象实例化 。 一个对象由两部分组成:第一个称为Data ,第二个称为Method或Function

"puts" and "gets" are the two most commonly used methods in our Ruby program. Let us see, which object they belong to.

“ puts”“ gets”是我们的Ruby程序中两个最常用的方法。 让我们看看它们属于哪个对象。

Observe the following codes:

遵守以下代码:

Code 1

代码1

puts "What is your name?"
name = gets.chomp
puts "Hi #{name}, Have a great day!!"

Code 2

代码2

Kernel.puts "What is your name?"
name = Kernel.gets.chomp
Kernel.puts "Hi #{name}, Have a great day!!"

You will find that puts and gets can also be used with a word known as Kernel and that Kernel is the actual object which encapsulates both the methods just like we have System.println in Java and Console.writeln in C#. puts and gets method is a part of the Kernel module.

您会发现puts和gets也可以与称为Kernel的单词一起使用,并且Kernel是封装这两种方法的实际对象,就像Java中的System.println和C#中的Console.writeln一样 。 puts和gets方法是Kernel模块的一部分。

We can also calculate the size of a string in the following manner.

我们还可以按照以下方式计算字符串的大小。

Kernel.puts "What is your name?".size

Output

输出量

18

If you have done programming in other languages, then the above might be confusing for you as String is a primitive data type and does not have its methods. But we know that Ruby is different, Ruby employs String as a full object which has its methods. In the above code, we are using one of its methods size which eventually returns the size of the string.

如果您已完成其他语言的编程,那么上面的内容可能会使您感到困惑,因为String是原始数据类型,并且没有其方法。 但是我们知道Ruby是不同的,Ruby将String用作具有其方法的完整对象。 在上面的代码中,我们使用其方法size之一,该方法最终返回字符串的大小。

Now, give String some rest and let us talk about Integers. Integers also come under primitive datatype in other languages but again in Ruby they are purely objects with certain methods. Let us see some of its methods.

现在,给String休息一下,让我们谈谈整数。 整数在其他语言中也属于原始数据类型,但在Ruby中,它们又纯粹是具有某些方法的对象。 让我们看看它的一些方法。

puts 3.object_id
puts 4.even?
puts 9.odd?
puts 7.class

Output

输出量

7
true
true
Integer

Observe the above code carefully, you will find for methods of Integer object namely:

仔细观察上面的代码,您会发现Integer对象的方法是:

  1. .object_id

    .object_id

    Each object has got an id. This method object_id returns an id associated with the object.

    每个对象都有一个ID。 此方法object_id返回与该对象关联的ID。

  2. .even?/.odd?

    .even?/。odd?

    The method checks whether the integer is even or odd and returns the value as True or False.

    该方法检查整数是偶数还是奇数,并将值返回为True或False。

  3. .class

    。类

    This method returns the class from which the object belongs.

    此方法返回对象所属的类。

对象创建 (Object creation)

There are two ways in which an object can be created. Implicit object creation is done with the help of literal notation. Explicit object creation is the object by a "new" keyword. Custom objects are created with the help of a "new" keyword.

有两种创建对象的方法。 隐式对象的创建是在文字符号的帮助下完成的。 显式对象创建是通过“ new”关键字创建的对象。 使用“ new”关键字来创建自定义对象

Consider the example given below for a better understanding of the concept,

考虑下面给出的示例,以更好地理解该概念,

str = String.new "Include Help"
puts str.size
puts str.upcase
puts str.downcase

Output

输出量

12
INCLUDE HELP
include help

The above example demonstrates the formal way of creating a String object. It is also showing some of the methods of the String object.

上面的示例演示了创建String对象的正式方法。 它还显示了String对象的一些方法。

翻译自: https://www.includehelp.com/ruby/objects.aspx

ruby 新建对象

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值