-
Notifications
You must be signed in to change notification settings - Fork 0
/
book_ruby.rb
38 lines (30 loc) · 855 Bytes
/
book_ruby.rb
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
def method
puts 'methods'
end
method
def encrypt_this t
t.gsub(/\b(\w)((\w)(\w*)(\w))?/){|t| "#{$1.ord}#{$5}#{$4}#{$3}" }
end
p encrypt_this "Hello 4 this is a my aloha to you"
########################################
class String
def encrypt_this1
chrs = self.chars
[chrs.shift.ord, chrs.pop, chrs[1..-1], chrs.shift].join
end
end
def encrypt_this1(text)
text.split.map(&:encrypt_this1).join(' ')
end
p encrypt_this1 "Hello 4 this is a my aloha to you"
#########################################
class String
def encrypt_this2
self.gsub(/\b(\w)((\w)(\w*)(\w))?/){|x| "#{$1.ord}#{$5}#{$4}#{$3}"}
#self.gsub(/(\w)(\w)?(\w+?)?(\w)?\b/) { "#{$1.ord}#{$4}#{$3}#{$2}" }
end
end
def encrypt_this2(text)
text.encrypt_this2
end
p encrypt_this2 "Hello 4 this is a my aloha to you"