/ / Ruby Array - Beginner Apprendimento su array e metodi - ruby, array, metodi

Ruby Array - Beginner Apprendimento su array e metodi - ruby, array, metodi

Sto cercando di superare una domanda in un tutorial di Ruby in cui si chiede quanto segue:

Crea un metodo chiamato new_array. Dovrebbe richiedere quattro argomenti e restituire un array con gli stessi argomenti. Inizia definendo la struttura del metodo: **

def new_array(a,b,c,d)
# return an array consisting of the arguments here
end

Quindi fornisce Specifiche per decodificare il problema. **

describe "new_array" do
it "creates an array of numbers" do
new_array(1,2,3,4).should eq([1,2,3,4])
end
it "creates an array of strings" do
new_array("a", "b", "c", "d").should eq(["a", "b", "c", "d"])
end
end

describe "first_and_last" do
it "creates new array with numbers" do
first_and_last([1,2,3]).should eq([1,3])
end
it "creates new array with strings" do
first_and_last(["a", "b", "c", "d"]).should eq(["a", "d"])
end
end

Devi trovare quanto segue dalla tua soluzione:

  • new_array crea una matrice di numeri
  • new_array crea una serie di stringhe

Finora ho il seguente codice, ma non riesco a mettere un altro array all'interno del metodo new_array in modo che possa anche produrre una serie di stringhe.

def new_array(a,b,c,d)
# return an array consisting of the arguments here
numbers = [1,2,3,4]
string = ["a","b","c","d"]


end

new_array(1,2,3,4)
new_array("a","b","c","d")

Come posso prendere quattro argomenti e restituire un array con lo stesso argomento all'interno della struttura del metodo sopra?

risposte:

0 per risposta № 1

Appena

def new_array(a,b,c,d)
[a, b, c, d]
end

Per argomenti illimitati, puoi anche fare:

def new_array(*arguments)
arguments
end

0 per risposta № 2

dove hai:

def new_array(a,b,c,d)
# return an array consisting of the arguments here
numbers = [1,2,3,4]
string = ["a","b","c","d"]
end

il tuo codice è preciso, ma non hai restituito nulla. prova ad aggiungere una linea in più che dice numbers per restituirlo. così:

def new_array(a, b, c, d)
# return an array consisting of the arguments here
numbers = [1, 2, 3, 4]
end

potrebbe funzionare, ai fini del tuo tutorial. in pratica, la risposta di xdazz è corretta al 100%


0 per risposta № 3

Sembra che il tutorial si aspetti due metodi: first_and_last e new_array.

first_and_last farà sempre la stessa cosa, quindi lo definiremo per primo.

def first_and_last(*args)
[args[0], args[-1]] #returns a new array containing first and last arguments
end

Per new_array hai due opzioni. Uno che prenderà solo 4 argomenti e uno che prenderà un numero illimitato.

def new_array(a, b, c, d) #only takes four arguments
[a, b, c, d] #returns a new array containing those arguments in order given
end

def new_array(*args) #takes unlimited arguments
args #same thing, but more efficient on typing
end

Lo stesso vale per first_and_last ovviamente, seguendo un formato simile. Tuttavia, questo dovrebbe superare le specifiche fornite.

Inoltre, sembra che il metodo che hai scritto sia solo un po 'spento. Ruby restituisce l'ultimo valore restituito da qualsiasi operazione nel metodo e l'impostazione di un valore restituisce anche tale valore come eval te lo dirò. Ad esempio, il codice x = 3 restituisce il valore intero 3. Quindi da quando hai definito il tuo new_array metodo per definire solo un numero e un array di stringhe e l'array di stringhe è stato definito secondo, restituirà SEMPRE il valore dell'array di stringhe.