<commit_before># Multidimensional array for Ruby

class NArray < Array
	attr_reader :dimensions
	def initialize dimensions, values = nil
		if dimensions.is_a? Array and dimensions.each { |d| NArray.valid_dimension? d }
			@dimensions = dimensions.length
			if @dimensions > 1 
				super(dimensions[0]).map! { |e| NArray.new(dimensions.slice(1..-1), values) }
			else
				super(dimensions[0], values)
			end
		elsif NArray.valid_dimension? dimensions
			@dimensions = dimensions
		else
			raise "Invalid dimensions"
		end
	end

	def [] position, *positions
		raise "#{positions.length + 1} positions instead of #{dimensions}" unless positions.length + 1 == dimensions
		positions.empty? ? super(position) : super(position)[*positions]
	end

	def []= position, *positions, value
		raise "#{positions.length + 1} positions instead of #{dimensions}" unless positions.length + 1 == dimensions
		if positions.empty? 
			super position, value
		else
			_at(position)[*positions] = value
		end
	end

	def length d = 0
		raise "Invalid dimension" if d < 0 or d > dimensions - 1
		d == 0 ? super() : _at(0).length(d - 1)
	end

	private
	def self.valid_dimension? dimension
		dimension.is_a? Integer and dimension >= 0
	end

	def _at i
		Array.instance_method(:[]).bind(self).call i
	end

end


n = NArray.new([3, 2, 4], 42)
n[0, 1, 3] = "a"
n[0,0,0] = 0
puts n.inspect
for i in 0...n.dimensions
	puts "dimension #{i}.length == #{n.length(i)}"
end<commit_msg>Complete the constructor and add #at<commit_after># Multidimensional array for Ruby

class NArray < Array
	attr_reader :dimensions
	def initialize dimensions, values = nil, &blck
		if dimensions.is_a? Array and dimensions.each { |d| NArray.valid_dimension? d }
			@dimensions = dimensions.length
			if @dimensions > 1 
				super(dimensions[0]).map! { |e| NArray.new(dimensions.slice(1..-1), values, &blck) }
			else
				if block_given?
					super(dimensions[0], &blck)
				else
					super(dimensions[0], values)
				end
			end
		elsif NArray.valid_dimension? dimensions
			@dimensions = dimensions
			if @dimensions > 1
				super(1) { NArray.new(dimensions - 1, values, &blck) }
			else 
				if block_given?
					super(0, &blck)
				else
					super(0, values)
				end
			end
		else
			raise "Invalid dimensions (must be a strictly positive integer or an array of lengths)"
		end
	end

	def [] position, *positions
		positions.empty? ? super(position) : super(position)[*positions]
	end

	def []= position, *positions, value
		raise "#{positions.length + 1} positions instead of #{dimensions}" unless positions.length + 1 == dimensions
		if positions.empty? 
			super position, value
		else
			_at(position)[*positions] = value
		end
	end

	def at(position, *positions)
		raise "#{positions.length + 1} positions instead of #{dimensions}" unless positions.length + 1 == dimensions
		
	end

	def length d = 0
		raise "Invalid dimension" if d < 0 or d > dimensions - 1
		d == 0 ? super() : _at(0).length(d - 1)
	end

	private
	def self.valid_dimension? dimension
		dimension.is_a? Integer and dimension >= 0
	end

	def _at i
		Array.instance_method(:[]).bind(self).call i
	end

end

n = NArray.new([3, 2, 4]) { [*(1..100)].sample }
n2 = NArray.new(2, 42)
puts "#{n2[0, 0] = 42}"
puts "#{n2}"

n[0, 1, 3] = "a"
n[0,0,0] = 0
puts n.inspect
for i in 0...n.dimensions
	puts "dimension #{i}.length == #{n.length(i)}"
end

n.each do |e| puts "Hola #{e}!" end
{'fraction_non_alphanumeric': 0.1312923167500736, 'fraction_numerical': 0.022372681778039447, 'mean_word_length': 3.598105548037889, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2585948', 'n_tokens_mistral': 1360, 'n_tokens_neox': 1158, 'n_words': 359}