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]"
--[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)
Java array
Recommended Posts