First part is the method and the second is the array and hash variables.
The manor in which Ruby returns values from a method is either by the return keyword or by the last executed line of code, thus if the result for the return of the method is the last line then there is no need to use the return keyword, but of course if the returnable value is calculated within the middle of the method and the rest is to clean up the method then use the return keyword.
# this is a basic method def saySomething(name) result = "hi there #{name}" return result end # but Ruby will return the last evaluated line, thus this method # is the same as the above one. def saySomething2(name) "hi there #{name}" end # the two methods that are the same in there results puts saySomething("Ian") puts saySomething2("Ian Porter") |
Below is how Ruby uses the array structure and also the hash structure, an array is defined within the [ ] brackets and the start of the array is 0, but within a hash since there is no start to the hash keys, then there is an hash key and then the values associated with that key. The hash structure is defined within { } brackets.
# this is a array a = ['Gentoo Linux','is','great'] #this is a hash, an hash is a key -> value has = { 'Gentoo' => 'Gentoo Linux', 'RH' => 'Red Hat Linux'} # hash puts a[0] # print the first element of the array puts a # print the arary puts "The Key value of RH within the hash" puts has['RH'] # print the value assoicated with the key RH puts "The hash in full" puts has |
Once executed from the console, the output is
hi there Ian hi there Ian Porter
for the first method demonstrating that the result value is the same
Gentoo Linux Gentoo Linux is great The Key value of RH within the hash Red Hat Linux The hash in full RHRed Hat LinuxGentooGentoo Linux
Which shows the results of the program, this shows that the two methods return in the same manor and also an array is broken into separate blocks, but a hash has a continuous memory block in nature.