Last active
August 29, 2015 13:56
-
-
Save boddhisattva/9081533 to your computer and use it in GitHub Desktop.
Arrays in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 | |
6 | |
Using size(above) n length(below) properties to print no. of elements in a string | |
6 | |
tryin to print anything outside array bounds ..like value of a[8] prints nill.. do note.. | |
11 | |
note how through a[-3] we get the value of the third last element of the array 7 | |
changing value of 1st element of array to 25.. and printing it.. note the way its printed..25 | |
Dyanmically do note how the size of an array can be increased in Ruby..Making things simpler n Faster..and a programmer can't be happier.. Yukihiro Matsumto | |
value of a[6]: zero | |
Also note how ruby is allowing the programmer to have an array with diverse data types... | |
to print the elements within the range of a[9]26 | |
27 | |
28 | |
29 | |
30 | |
to print the first two elements of an array | |
25 | |
3 | |
Values of array before change within a specified range..: | |
[3, 5, 7, 9, 11, "zero", nil, nil, [26, 27, 28, 29, 30]] | |
changing values of an array within a specified range | |
[5, "Yapdi", "Irika", 11] | |
Note the difference between the above op and the op below due to the diff of just one '.' in the range 2 to 5 | |
[5, "Yapdi", "Irika"] | |
true | |
true | |
true | |
does the array of r include the symbol l?: false | |
a[7] is nil? ans: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# working wit arrays - covering basic examples .. not all.. | |
#-------------------------------------------------------------------------------------------------------------------------------- | |
a = [ 1,3,5,7,9,11 ] | |
puts(a[0]) # prints the 1st element value of the array... | |
#-------------------------------------------------------------------------------------------------------------------------------- | |
print( a.size ) # prints the length of the array... | |
puts("\nUsing size(above) n length(below) properties to print no. of elements in a string\n") | |
print( a.length ) | |
#-------------------------------------------------------------------------------------------------------------------------------- | |
print(a[8]) # doesn't work..but the below will work.. | |
#-------------------------------------------------------------------------------------------------------------------------------- | |
print("\ntryin to print anything outside array bounds ..like value of a[8] prints nill.. do note.. ") | |
puts(a[8]) | |
puts(a[-1]) | |
#-------------------------------------------------------------------------------------------------------------------------------- | |
print(" note how through a[-3] we get the value of the third last element of the array ") | |
puts(a[-3]) | |
#-------------------------------------------------------------------------------------------------------------------------------- | |
print("changing value of 1st element of array to 25.. and printing it.. note the way its printed..") | |
a[0] = 25 | |
puts(a[-a.size]) | |
#-------------------------------------------------------------------------------------------------------------------------------- | |
print("Dyanmically do note how the size of an array can be increased in Ruby..Making things simpler n Faster..\ | |
and a programmer can't be happier.. Yukihiro Matsumto\n") | |
a[6] = "zero" | |
puts("value of a[6]:\t #{a[6]} ") | |
print("Also note how ruby is allowing the programmer to have an array with diverse data types... \n") | |
#-------------------------------------------------------------------------------------------------------------------------------- | |
a[9] = (26..30).to_a | |
print("\nto print the elements within the range of a[9]") | |
puts(a[9,]) | |
#-------------------------------------------------------------------------------------------------------------------------------- | |
print("\nto print the first two elements of an array\n") | |
puts(a[0..1]) | |
#-------------------------------------------------------------------------------------------------------------------------------- | |
#how to print array elements within a[9] .. upto a specific range..?? | |
#print("to print the elements within the range of a[9]") | |
#puts(a[9,[1,2]]) | |
#??????? | |
#-------------------------------------------------------------------------------------------------------------------------------- | |
puts("\n Values of array before change within a specified range..: \n #{a[1..9]} ") | |
print("\nchanging values of an array within a specified range") | |
a[3..4] = ['Yapdi','Irika'] | |
#-------------------------------------------------------------------------------------------------------------------------------- | |
puts("\n #{a[2..5]} ") | |
print("\n Note the difference between the above op and the op below due to the diff of just one '.' in the range 2 to 5 \n") | |
puts("\n #{a[2...5]} ") | |
#-------------------------------------------------------------------------------------------------------------------------------- | |
# now dealing mainly with ranges, true, false and nil.. | |
r = 'a'..'c' | |
puts(r.include? 'a') | |
puts(r.member? 'b') #member and include are like synonyms.. do note.. | |
puts(r.cover? 'c') # even the cover works the same.. as member n include.. | |
print("does the array of r include the symbol l?: ") | |
puts(r.include? 'l') | |
print("\na[7] is nil? ans: ") | |
puts(a[7].nil?) | |
#-------------------------------------------------------------------------------------------------------------------------------- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment