Ruby-Basics Vol 2
Hi Everyone!
This our Second Fastrack series in which we will teach you the basics in a fastway..
Today we will learn the basics of Ruby.
This is Volume 2.
Content:
You can Types these commands on terminal (ruby interpreter) by typing
$ irb
irb(main):001:0>
Arrays
- Arrays are lists in ruby, means they doesn't need to be homegenous.
a = [] #This creates an Empty Array
a = ["Amit", 1, [2,5,6]]
Output:
[]
["Amit",1,[2,5,6]]
- Arrays are continuous data structures
$ irb
> a = []
[]
> a[45] = 5
> puts a
[nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, 5]
- a[45] = 0, created 45 empty spaces and then added 5 at 45th index. Indexing starts from zero.
$ irb
> puts a[45]
5
> puts a[0] # Would print nothing
> puts a.length
46
Sets
- Sets are used to keep unique elements, no duplicates are allowed.
my_set = Set.new() # New Empty Set
my_set.add(1) # Add one Element
my_set.each do |v|
puts v
end
my_set.add([1,2,3,4,5]) # adding [1,2,3,4,5] as a single element
my_set.each do |v|
puts v
end
Output :
1
1
[1, 2, 3, 4, 5]
#<Set: {1, [1, 2, 3, 4, 5]}>
- Another way to create a Set
Way 1
a = [1,2,3,4,5,1,2,3,4,5]
my_set = a.to_set # Easy way to create a set from array
puts my_set.size
Output :
5
Way 2 :
my_set = Set.new([1,2,3,1,2,3]) # Easy way to create a set while initializing
Output:
#<Set: {1, 2, 3}>
Hashes
- Hashes are smilar to javascript dictionaries, the only difference is that they are ordered.
$ irb
> my_hash = {} # creates a new Hash
{}
> my_hash = Hash.new # creates a new Hash
{}
> my_hash = {:name => "Amit", :city => "ABC"} # Creates a hash
{:name=>"Amit", :city=>"ABC"}
> my_hash[:name] # to access :name from my_hash
"Amit"
- :name and :city are actually symbols. they are used for faster accessing, we may use strings as well.
> my_hash = {"name" => "amit"}
{"name"=>"amit"}
> puts my_hash["name"]
amit
> puts my_hash[:name] # This won't work, you need to remember that strings and symbols are different
> my_hash = {:name => "Amit", :city => "Abc", :age => 21}
{:name=>"Amit", :city=>"Abc", :age=>21}
> my_hash.each do |key| # iterating over the keys
>* print key,"\n"
>* end
[:name, "amit"]
[:city, "Abc"]
[:age, 25]
=> {:name=>"amit", :city=>"Abc", :age=>21}
- If you look at the above code, the keys are ordered.
> my_hash.each do |key,value|
>* puts "#{key} : #{value}"
>* end
name : amit
city : Abc
age : 21
{:name=>"amit", :city=>"Abc", :age=>21}
Methods
Example:
def my_method
puts "hello method"
end
my_method()
Output:
hello method
$ irb
> def is_odd number # parenthesis are optional
>* return number % 2 == 1
>* end
> is_odd 1 # parenthesis are optional
true
> def is_even number
>* number % 2 == 0 #last expression in every function is a return statement by default.
>* end
:is_even
> is_even 2
true
> def join_words words_array
>* result = ""
>* words_array.each do |word|
>* result = result + word + " " # String concatenation
>* end
>* result
>* end
> puts join_words ["Ruby","is","flexible"]
Ruby is flexible
- What if you want to pass a variable number of arguments and you don't want to pass an array.
Voila, we have *args argument.
> def join_words *args
>* result = ""
>* args.each do |word|
>* result = result + word + " " # String concatenation
>* end
>* result
>* end
> puts join_words "Ruby","is","flexible" # This code is more verbose
Ruby is flexible
> puts join_words "Ruby","is","flexible",",", "This", "code" ,"is" ,"more" ,"verbose"
Ruby is flexible , This code is more verbose
If you like such articles , please follow, share , comment and upvote to show your support.
Congratulations @kingofpirate! You received a personal award!
You can view your badges on your Steem Board and compare to others on the Steem Ranking
Vote for @Steemitboard as a witness to get one more award and increased upvotes!
Upvote this for me please? https://steemit.com/christianity/@bible.com/verse-of-the-day-hebrews-4-16-nlt