forked from indravesh/Ruby-Python-Practice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruby-basics.txt
60 lines (53 loc) · 2.37 KB
/
ruby-basics.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Ruby - is a opps language just like python. Have its own interpreter to execute the code.
- Easy to install
- code are small and easy to maintain
- rich in-built function
- can be used to make small nodes, like internet and intranet
- scripting language
Principle -
Encapsulation -
Binding data and method together.
Example -
Before we start understanding Encapsulation, we have to understand what is data and and what is method.
data - a value , which can be store in a variable for further use.
method - an object, fucntion.
Lets see how to store a value in a variable -
a = 5 # a is a variable and 5 is given to a as a value wiht the help of '= 'which is an assignment operator
Lets see how to define a method -
use a def keyword to define a method in ruby .
Syntax :
def method_name # method name can be in lowercase or using camelCase
logic
end # end is enclosure
Polymorphism
multiple forms just like + and *
1 + 1 => 2 # here + operator is working as additional operator
"1" + "1" => "11" # here + operator is working as concatenational operator
"h" * 3 => "hhh"
1.+(1)
object.function(arg)
Inheritance
ruby does not support multiple inheritance
Lets see how to define a class
class ClassName # class is a keyword and next to it is className which must start with upercase
OBJECT # group of methods
end # end is enclosure
class Apple
def iPhone
puts "I love iPhoneX"
end
def iPod
puts "Good for music lover, easy to carry"
end
end
# Apple is a class which have two objects iPhone and iPod to access these objects first you have to create/initialize the class or create a constructor of class
m = Apple.new # here new method will help you to initialize the class
m.iPhone
m.iPod
Abstraction
Assignment -
Find out the missing elements from the given input(take the input from command line)
Sample input - [2, 4, 6, 8, 12, 14, 18]
Sample Output 10, 16
python3 file.py "[2, 4, 6, 8, 12, 14, 18]"
Rails -