Skip to content

Commit 627d635

Browse files
committed
DevSession: add exercise files
1 parent 129e7cc commit 627d635

File tree

8 files changed

+188
-0
lines changed

8 files changed

+188
-0
lines changed

DevSession/Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
gem 'pry'

DevSession/Gemfile.lock

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
GEM
2+
specs:
3+
coderay (1.1.3)
4+
method_source (1.0.0)
5+
pry (0.14.1)
6+
coderay (~> 1.1)
7+
method_source (~> 1.0)
8+
9+
PLATFORMS
10+
arm64-darwin-20
11+
12+
DEPENDENCIES
13+
pry
14+
15+
BUNDLED WITH
16+
2.3.18

DevSession/demo.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
require 'pry'
2+
3+
class User
4+
attr_reader :args
5+
6+
def initialize(**args)
7+
@args = args
8+
end
9+
10+
def save!
11+
users_db.write({ id: find_id, name: args[:name]}.to_json)
12+
users_db.write("\n")
13+
end
14+
15+
def find_id
16+
binding.pry
17+
users_db.read.split('\n').last[:id] + 1
18+
end
19+
20+
def users_db
21+
File.open('users.json', 'a+')
22+
end
23+
end
24+
25+
User.new(id: 1, name: 'Istiak').save!

DevSession/employee.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
require './user'
2+
3+
class Employee < User
4+
attr_accessor :dept
5+
6+
def initialize(name: , age: , dept:)
7+
@dept = dept
8+
super
9+
end
10+
11+
def age
12+
p = Proc.new { "age is #{self.age}" }
13+
super(p)
14+
end
15+
end
16+
17+
# puts "Please enter name: "
18+
# name = gets
19+
20+
# puts "Please enter age: "
21+
# age = gets
22+
23+
# puts "Please enter dept: "
24+
# dept = gets
25+
26+
emp = Employee.new(name: 'Test', age: 20, dept: 'JS')
27+
28+
puts emp.dept, emp.name, emp.age

DevSession/test.rb

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2+
class String
3+
def empty?
4+
!self
5+
end
6+
7+
def blank?
8+
9+
end
10+
11+
def split_by_space
12+
self.split(' ')
13+
end
14+
15+
def method_missing(block)
16+
if block.to_s == 'no_method'
17+
puts "Please define your method"
18+
else
19+
super
20+
end
21+
end
22+
end
23+
24+
class Test
25+
class NotFoundError < StandardError
26+
def initialize(key: )
27+
@key = key
28+
end
29+
30+
def to_s
31+
"The key: #{@key} you are looking for can not be found! "
32+
end
33+
end
34+
35+
def calculate(**args)
36+
error_handler do
37+
if args[:name].blank?
38+
raise NotFoundError.new(key: 'name')
39+
end
40+
41+
puts 100 / 8
42+
end
43+
end
44+
45+
def error_handler
46+
yield
47+
rescue ZeroDivisionError
48+
puts "You can not devide by zeror"
49+
rescue NotFoundError => e
50+
puts e.to_s
51+
rescue StandardError => e
52+
puts "Unkown error! please check"
53+
puts e.inspect
54+
end
55+
end
56+
57+
puts "This is my string".no_method_1
58+
Test.new.calculate(name: '')

DevSession/user.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{:id=>1, :name=>"Istiak"}

DevSession/user.rb

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
class User
2+
attr_accessor :name, :age
3+
4+
@@counter = 0
5+
6+
def initialize(name:, age:, **args)
7+
@name = name
8+
@age = age
9+
@args = args
10+
puts args.inspect
11+
end
12+
13+
def self.counter
14+
@@counter
15+
end
16+
17+
def self.counter=(arg)
18+
@@counter += 1
19+
end
20+
21+
def age(p)
22+
p.call
23+
end
24+
25+
# def name
26+
# @name
27+
# end
28+
29+
# def age
30+
# @age
31+
# end
32+
33+
# def name=(arg)
34+
# @name = arg
35+
# end
36+
37+
# def age=(arg)
38+
# @age = arg
39+
# end
40+
end
41+
42+
# puts User.counter
43+
44+
# user = User.new(name: 'Istiak', age: 10)
45+
# user2 = User.new(name: 'Mujhahid', age: 20)
46+
# puts user.inspect
47+
# puts user.name
48+
# puts user.age
49+
# user.name = 'Sadman'
50+
51+
# puts user.name
52+
# puts user.inspect
53+
54+
# puts User.counter

DevSession/users.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{:id=>1, :name=>"Istiak"}
2+
{:id=>1, :name=>"Istiak"}
3+
{:id=>1, :name=>"Istiak"}
4+
{:id=>1, :name=>"Istiak"}
5+
{:id=>1, :name=>"Istiak"}

0 commit comments

Comments
 (0)