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) Explain calling super and calling super() and give some example? | |
super and super() both inherits methods from parent class. super allows the arguments but super() dont allow the arguments. | |
Ex: | |
-- | |
class Niyati | |
def process(person,work) | |
"I am #{person} and am a #{work}" | |
end | |
end | |
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
=> name/value pairs | |
{ | |
"name":"value" | |
} | |
=> supports | |
number, strings, boolean, nil, array | |
=> | |
{ | |
"name":"value", | |
"array": [ 1,2,3 ], |
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
arr = [4,1,2,7,"M","A","V"] | |
arr.partition { |a| a.is_a? String }.map(&:sort).flatten | |
p arr | |
=> | |
["A","V","M",1,2,4,7] | |
------- | |
a = [1, "A", nil, "M", "", ''] | |
a.reject!{|a| a.nil? || a.to_s.empty? } | |
p a | |
=> |
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
movies = { | |
Ravanan: 4.8, | |
Thanioruvan: 4.5, | |
payanangal: 4 | |
} | |
puts "What would you like to do?" | |
puts "-- Type 'add' to add a movie." | |
puts "-- Type 'update' to update a movie." | |
puts "-- Type 'display' to display all movies." |
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
arr = [1,2,3,4,5] | |
arr.each { |n| n + 2 } | |
arr | |
=> [1,2,3,4,5] | |
arr.map! { |n| n + 2 } | |
arr | |
=> | |
[3,4,5,6,7] | |
arr.collect! { |n| n + 2 } | |
arr |
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
#reduce | |
def sum(*args) #sum method accepts many number of arguments that method passed | |
args.reduce(0, :+) #reduce will reduce the array in single number, 0 is the initial value and + tells the reduce method to perform addition | |
end | |
sum(1,2,3,4) | |
=> | |
10 | |
----- | |
multiplication |
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
l = -> (name) { puts "#{name}" } | |
l.call("velu") | |
=> | |
velu | |
-------- | |
l = -> (num) { num * 5 } # l is the variable which stores the block of code, when we calls the l the block of code will be executed. | |
l.call(5) #Another important thing is lambda is an object. | |
=> | |
25 |
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
p = Proc.new do #proc is a object, actually a block of code stored in a instance variable | |
"hello" | |
end | |
p.call | |
----- | |
p = Proc.new do |num| | |
num * num | |
end |
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
class Work | |
def initialize(name) | |
@name = name | |
end | |
def name | |
@name | |
end | |
def name=(name) | |
@name = name | |
end |
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
class Work | |
attr_accessor :name, :duration | |
end | |
job = Work.new("ROR learning", "1month") | |
job.name | |
=> "ROR learning" | |
job.name = "Ruby learning" | |
job.name | |
=> "Ruby learning" |
NewerOlder