DEV Community

Cover image for Learning ruby - day 1
aderayevans
aderayevans

Posted on

Learning ruby - day 1

Table of Contents

  1. Introduction
  2. Content
  3. Conclusion

Introduction

Hey guys, welcome to my blog recording my ruby self-taught process

I decided to learn this amazing language while I was trying to solving some ruby challenges to get my tenth badge in HackerRank

Ok let's do it


Create my first program in ruby

print "Hello World!!!"
Enter fullscreen mode Exit fullscreen mode

Output

PS D:\Workplace> ruby hello-world.rb
Hello World!!!
Enter fullscreen mode Exit fullscreen mode

print "\n" equals puts

print "First line "
print "still first line\n"
puts "Second line"
puts "Thirld line"
Enter fullscreen mode Exit fullscreen mode

Output

First line still first line
Second line
Thirld line
Enter fullscreen mode Exit fullscreen mode

Everything is object

print else
Enter fullscreen mode Exit fullscreen mode

Output

main
Enter fullscreen mode Exit fullscreen mode

Call Object Method

check whether the num is even or odd

num = 2
print num.even?
Enter fullscreen mode Exit fullscreen mode

Output

true
Enter fullscreen mode Exit fullscreen mode

Call Object Method Parameters

print the quotient of num and 2

num = 2
num.div 2
print num
Enter fullscreen mode Exit fullscreen mode

Output

1
Enter fullscreen mode Exit fullscreen mode

First loop

print the value of each member in array to the power of 2

array = [1, 2, 3, 4, 5]
array.each do |num|
    puts num.pow 2
end
Enter fullscreen mode Exit fullscreen mode

Output

1
4
9
16
25
Enter fullscreen mode Exit fullscreen mode

First function

print ASCII chars of any decimal number in array if number is not odd

def print_char(array)
    array.each do |num|
        puts num.chr unless num.odd?
    end
end
array = [65, 66, 67, 68, 69, 70, 71]
print_char(array)
Enter fullscreen mode Exit fullscreen mode

Output

B
D
F
Enter fullscreen mode Exit fullscreen mode

Array

Working on single element

Delete last element

arr.pop
Enter fullscreen mode Exit fullscreen mode

Delete first element

arr.shift
Enter fullscreen mode Exit fullscreen mode

Delete element at position a given position

arr.delete_at(2)
Enter fullscreen mode Exit fullscreen mode

Working on multiple elements

Delete all occurrences of a given element

arr.delete(5)
Enter fullscreen mode Exit fullscreen mode

Delete elements if

arr.delete_if {|a| a < 2}
Enter fullscreen mode Exit fullscreen mode

Delete elements unless

arr.keep_if {|a| a < 4}  
Enter fullscreen mode Exit fullscreen mode

Create subarray

Select elements from array to subarray

arr2 = arr.select {|a| a > 2}
print arr2
Enter fullscreen mode Exit fullscreen mode

Reject elements from array to subarray

arr2 = arr.reject {|a| a > 2}
Enter fullscreen mode Exit fullscreen mode

Reject elements from array till returns false for the first time

arr2 = arr.drop_while {|a| a > 1}
Enter fullscreen mode Exit fullscreen mode

Hash, a dictionary-like data structures

# initialize and set default value
empty_hash = Hash.new 
empty_hash.default = 1
# or
default_hash = Hash.new(1)
Enter fullscreen mode Exit fullscreen mode

Set value

new_hash = {"key1" => 12, "key2" => 50}
# or
new_hash = Hash.new
new_hash["key1"] = 12
new_hash["key2"] = 50
# or
new_hash.store("key1", 12)
new_hash.store(1512032, 50)
Enter fullscreen mode Exit fullscreen mode

Loop with hash

hash.each do |key, value|
    puts key
    puts value
end
# or
hash.each do |array|
    # now key is array[0], value is array[1]
    puts array[0]
    puts array[1]
end
Enter fullscreen mode Exit fullscreen mode

Delete pair

# Delete specific key
hash.delete("key1")
# Delete if
hash.keep_if {|key, value| key.is_a? Integer} 
# or 
hash.delete_if {|key, value|key.even? }
Enter fullscreen mode Exit fullscreen mode

Infinite Loop

i = 0
loop do
    puts i
    i += 1
    break if i == 10
end
Enter fullscreen mode Exit fullscreen mode

Output

1
2
3
4
5
6
7
8
9
10
Enter fullscreen mode Exit fullscreen mode


Conclusion

We have learned how to print output

How to do some basic loop and function

Learned some common methods of array and a dictionary-like data structures called hash

I'd like to thank HackerRank for giving me a chance to acknowledge and be interesting in this cool language

If guys found Ruby is fascinating, please come to the link below and solve some Ruby challenges with me

https://www.hackerrank.com/domains/ruby

If you have any question or suggestion, please let me know at the comment section below.

OK I'm going to wrap this up here

Thanks for reading my blog

Top comments (0)