Initialize Ruby array with 0 like Java, that is, set the default value to 0

I thought about it when I was copying Java. I think Ruby can also be initialized with 0.

I tried it.

Usually filled with nil.

[new (Array) --Ruby Reference] (https://ref.xaio.jp/ruby/classes/array/new)

The size of the array (number of elements) can be specified as an integer in the argument size. If size is not specified, it will be an empty array. The second argument, obj, fills the new array with that object. If the second argument is not specified, it is filled with nil.

The trigger was this site.

[Set the default value of [Ruby] array to 0 | Informatics Finder] (http://frsw.net/blog/ruby%E9%85%8D%E5%88%97%E3%81%AE%E3%83%87%E3%83%95%E3%82%A9%E3%83%AB%E3%83%88%E5%80%A4%E3%82%920%E3%81%AB%E3%81%99%E3%82%8B/)

Hi, I was happy if I could set a default value when there is no value when accessing an element like a hash using Ruby, but it seems that there is no such function, so initial to 0 I found a way to make it.

Java

import java.util.*;

public class Main {
    public static void main(String[] args) {
    int[] a = new int[5];
        
    a[1] = 37;
    a[2] = 51;
    a[4] = a[1] * 2;
    
    System.out.println(a[0]);
    System.out.println(a[1]);
    System.out.println(a[2]);
    System.out.println(a[3]);
    System.out.println(a[4]);
    }
}
//#=>
/*
0
37
51
0
74
*/

The sutras were copied [Introduction to new and clear Java|Nobuhiro Shibata|programming|Kindle Store| Amazon] (https://www.amazon.co.jp/%E6%96%B0%E3%83%BB%E6%98%8E%E8%A7%A3Java%E5%85%A5%E9%96%80-%E6%9F%B4%E7%94%B0-%E6%9C%9B%E6%B4%8B-ebook/dp/B01HYSUY92)

Introduction to new and clear Java

I forgot how to write a multi-line comment. [[Java] Types of comments and how to write them --Qiita] (https://qiita.com/mtanabe/items/2e52e6e162d28df8437f)

How to write multi-line comments /*

Ruby

a = Array.new(5,0)

a[1]=37
a[2]=51;
a[4]=a[1]*2

p a[0]
p a[1]
p a[2]
p a[3]
p a[4]
#=>
__END__
0
37
51
0
74

one more

p Array.new(5) {|i| i = 0}
 
 #=> [0, 0, 0, 0, 0]

[Can I create an array in Ruby with default values? - Stack Overflow] (https://stackoverflow.com/questions/5324654/can-i-create-an-array-in-ruby-with-default-values)

Not auto extended, but initialized to the specified length with a default value:

>> Array.new(123, 0)  
=> [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

[Be careful not to put an object in the initialization of Ruby Array with the default value. · Shunsuke227ono / pelican Wiki] (https://github.com/shunsuke227ono/pelican/wiki/Ruby-Array%E3%81%AE%E5%88%9D%E6%9C%9F%E5%8C%96%E3%81%AF%E3%83%87%E3%83%95%E3%82%A9%E3%83%AB%E3%83%88%E3%83%90%E3%83%AA%E3%83%A5%E3%83%BC%E3%81%A7%E3%82%AA%E3%83%96%E3%82%B8%E3%82%A7%E3%82%AF%E3%83%88%E3%82%92%E5%85%A5%E3%82%8C%E3%81%AA%E3%81%84%E3%82%88%E3%81%86%E6%B0%97%E3%82%92%E3%81%A4%E3%81%91%E3%81%A6%E3%80%82)

a = Array.new(3, 0) # [0,0,0]

[Ruby Array --Qiita] (https://qiita.com/Shimba/items/24c70f4a176d2b731e95)

Array.new(5) {|i| i * 3}
 => [0, 3, 6, 9, 12]"

reference

--[new (Array) --Ruby Reference] (https://ref.xaio.jp/ruby/classes/array/new)

-[[Ruby] Set the default value of the array to 0 | Informatics Finder] (http://frsw.net/blog/ruby%E9%85%8D%E5%88%97%E3%81%AE%E3%83%87%E3%83%95%E3%82%A9%E3%83%AB%E3%83%88%E5%80%A4%E3%82%920%E3%81%AB%E3%81%99%E3%82%8B/)

-[[Java] Comment types and writing style-Qiita] (https://qiita.com/mtanabe/items/2e52e6e162d28df8437f)

-[Be careful not to put an object in the initialization of Ruby Array with the default value. · Shunsuke227ono / pelican Wiki] (https://github.com/shunsuke227ono/pelican/wiki/Ruby-Array%E3%81%AE%E5%88%9D%E6%9C%9F%E5%8C%96%E3%81%AF%E3%83%87%E3%83%95%E3%82%A9%E3%83%AB%E3%83%88%E3%83%90%E3%83%AA%E3%83%A5%E3%83%BC%E3%81%A7%E3%82%AA%E3%83%96%E3%82%B8%E3%82%A7%E3%82%AF%E3%83%88%E3%82%92%E5%85%A5%E3%82%8C%E3%81%AA%E3%81%84%E3%82%88%E3%81%86%E6%B0%97%E3%82%92%E3%81%A4%E3%81%91%E3%81%A6%E3%80%82)

-[Ruby array-Qiita] (https://qiita.com/Shimba/items/24c70f4a176d2b731e95)

What to do next

Java array

Recommended Posts

Initialize Ruby array with 0 like Java, that is, set the default value to 0
How to initialize Java array
Determine that the value is a multiple of 〇 in Ruby
How to retrieve the hash value in an array in Ruby
How to output the value when there is an array in the array
Replace with a value according to the match with a Java regular expression
The milliseconds to set in /lib/calendars.properties of Java jre is UTC
[Java] How to search for a value in an array (or list) with the contains method
Declare a method that has a Java return value with the return value data type
[WSL] Solution for the phenomenon that 404 is displayed when trying to insert Java with apt (personal memo)
[Introduction to Java] I tried to summarize the knowledge that I think is essential
[Java] How to set the Date time to 00:00:00
[Java small story] Monitor when a value is added to the List
How to get the value after "_" in Windows batch like Java -version
I tried to make a program that searches for the target class from the process that is overloaded with Java
[Java] Set the time from the browser with jsoup
Try to link Ruby and Java with Dapr
[Java] (for MacOS) How to set the classpath
Specify the default value with @Builder of Lombok
I want to get the value in Ruby
Speed comparison when the value side of Hash wants to retrieve all with array
How to request by passing an array to the query with HTTP Client of Ruby
Summary of how to use the proxy set in IE when connecting with Java
Output the maximum value from the array using Java standard output
[Java] How to get the maximum value of HashMap
[Java] Refer to and set private variables with reflection
[Ruby / Rails] Set a unique (unique) value in the class
HTTPS connection with Java to the self-signed certificate server
Addicted to the webpacker that comes standard with Rails 6
Null value is entered when assigning to an array
The story that .java is also built in Unity 2018
From Java to Ruby !!
[Java] Change the process according to the situation with the Strategy pattern
Review the basic knowledge of ruby that is often forgotten
When is the default value automatically entered without explicit initialization?
How to divide a two-dimensional array into four with ruby
Learning Ruby with AtCoder 13 How to make a two-dimensional array
[Java] [Play Framework] Until the project is started with Gradle
Now is the time to get started with the Stream API
Be sure to compare the result of Java compareTo with 0
Convert 2D array to csv format with Java 8 Stream API
[Java] Code that is hard to notice but terribly slow
[Java] UTF-8 (with BOM) is converted to 0xFFFD (REPLACEMENT CHARACTER)
The story of making a binding for libui, a GUI library for Ruby that is easy to install
How to make an application with ruby on rails (assuming that the environment has been built)
The story that the DB connection and other timeouts did not time out according to the set value
When I try to use the AWS SDK with Ruby + Lambda, `sam local` is messed up.
How to deal with the type that I thought about writing a Java program for 2 years