Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Manvendra committed Nov 22, 2018
0 parents commit 608de8e
Show file tree
Hide file tree
Showing 165 changed files with 2,482 additions and 0 deletions.
3 changes: 3 additions & 0 deletions 10F/NilClass.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
p
puts
print
8 changes: 8 additions & 0 deletions 10F/String.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
String - collection of characters

str = String.new
=> ""

str = ""

str = "Hello world"
9 changes: 9 additions & 0 deletions 10F/array.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Array - collection of similar data types

r = Array.new
=> []

r = []

r << 1
r << 2
58 changes: 58 additions & 0 deletions 10F/conditions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
if condition

if condition
logic # if the condition is true
end

OR

if condition
logic # if the condition is true
else
logic # if the condition is false
end


if condition1
logic # if the condition1 is true
elsif condition2
logic # if the condition1 is false and condition2 is true
elsif condition3
logic # if the condition1 and 2 is false and condition3 is true
else
logic # if all above conditions are false
end


unless condition
logic # if the condition is false
else
logic # if the condition is true
end


case varaible
when condition1
logic
when condition2
logic
else
no logic
end



while condition
logic # if the condition is true
end

a = 10
while a < 20
a += 1
puts "Hello"
end


until condition
logic #if the condition is false
end
Empty file added 10F/error_handling.rb
Empty file.
10 changes: 10 additions & 0 deletions 10F/fib.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def fib(n)
a, b = 0, 1
puts a
(1..9).each do | i |
puts b
a,b = b, a + b
end
end

fib(8)
18 changes: 18 additions & 0 deletions 10F/file_system.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
File - is class

method
read
write
open

mode
r
w
wr
a+
wb
rb

Q. take user name and email ans store into a file.


17 changes: 17 additions & 0 deletions 10F/hash.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Hash - store the value in key and value pair

h = Hash.new
=> { }

or

h = {}


HOw to put values inside the hash

h[:key] = value

How to get a value

h[:key_name]
Empty file added 10F/iterators.rb
Empty file.
31 changes: 31 additions & 0 deletions 10F/loops.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Iterators

each

map
collect


SYNtax :

collection.map/each/collect do | object|
logic
end


[1,2,3,4].map do | i |
i * 2
end




loop - Explained

for object in collection
logic
end

for i in [1,2,3,4]
puts i* 3
end
10 changes: 10 additions & 0 deletions 10F/palidrome.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def palindrome?(str)
if str == str.reverse
true
else
false
end
end

puts palindrome?("mom")
puts palindrome?("papa")
15 changes: 15 additions & 0 deletions 10F/prg1.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class A
def user
puts "Enter your name"
name = STDIN.gets.chomp
puts "Enter your address"
address = STDIN.gets.chomp
file = File.open('user_input', "a+")
file.puts name
file.puts address
file.close
end
end

r = A.new
r.user
2 changes: 2 additions & 0 deletions 10F/prg2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
r = File.read('user_input')
puts r.split("\n")
6 changes: 6 additions & 0 deletions 10F/range.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Range -
(source..destination) # include the destination
(source...destination) # exclude the destination

(1..10) => 1..10
(1...10) => 1..9
4 changes: 4 additions & 0 deletions 10F/user_input
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
UN
USA
Bangalore
ABC
16 changes: 16 additions & 0 deletions 10F/user_input.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

file = File.open("user_input2", "a+")

loop do
puts "Enter your name"
name = STDIN.gets.chomp
puts "Enter your address"
address = STDIN.gets.chomp
file.puts "#{name} #{address}"
puts "To continue please enter yes otherwise type anything to terminate"
response = STDIN.gets.chomp
if not response.match(/yes/i)
file.close
break
end
end
2 changes: 2 additions & 0 deletions 10F/user_input2
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
hello bye
world UN
41 changes: 41 additions & 0 deletions 11F/error_handling.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# begin
# code
# rescue
# code
# end

# [1,2,3,0,4,5,6].each do | i |
# begin
# puts 100/i
# rescue Exception => e
# puts e.message
# i = 2
# retry
# ensure
# puts "I am present"
# end
# end

begin
puts "Enter your name, excluding admin and super"
name = STDIN.gets.chomp
raise "super and admin can't be taken as name" if name.match(/admin|super/)
rescue Exception => e
puts e.message
retry
end



begin
code
rescue NoMethodError
code
end





puts x = 100/0 rescue nil
puts x = 100/2 rescue nil
10 changes: 10 additions & 0 deletions 12F/file1.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def find(str)
if str.match(/^(\d{2,3}.\d{2,3}.\d{1,3}.\d{1,3})\s(Safari|Chrome)\sUSA$/i)
puts "#{$1} and #{$2}"
end
end


File.read('log.txt').split("\n").each do | each_line |
find(each_line)
end
9 changes: 9 additions & 0 deletions 12F/file2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def email(str)
if str =~ /^[^!-)|_]\D.*@(gmail|yahoo).(in|com)/
puts "valid email - #{str}"
end
end

email("[email protected]")
email("[email protected]")
email("[email protected]")
5 changes: 5 additions & 0 deletions 12F/log.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
192.168.1.1 Safari USA
17.10.18.20 Chrome China
1.3.4.1 Mozila USA
17.10.18.22 Chrome USA
17.10.18.23 Mozila USA
8 changes: 8 additions & 0 deletions 12F/regex.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Regex is a pattern matching or finding logic for a given string


r =~ /patter/

or

r.mathc(/pattern/)
31 changes: 31 additions & 0 deletions 13F/.byebug_history
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
exit
x1.count
str_array.count("")
str_array
c
x2
str_array.count("")
str_array
c
x2
str_array
x1.count
str_array.count("")
c
x2
str_array
str_array.count("")
c
m
c
str_array.count("")
x2
str_array
c
x2
str_array
x1.count
str_array.count("")
c
x1.count
str_array.count("")
5 changes: 5 additions & 0 deletions 13F/a.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class A
def p1
"An instance method"
end
end
8 changes: 8 additions & 0 deletions 13F/b.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require_relative 'a'


class B < A
end

b = B.new
puts b.p1
16 changes: 16 additions & 0 deletions 13F/block.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
def p1(t)
t = 20 + t
yield(t) if block_given?
puts "From method p1"
yield(t + 50) if block_given?
end

p1(10) do | i |
puts "I am a block n value is #{i}"
end

#OR

p1(10) { | i |
puts "I am a block n value is #{i}"
}
8 changes: 8 additions & 0 deletions 13F/grep.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
irb(main):001:0> ["12", "Hello", "bye"].grep(/bye/)
=> ["bye"]
irb(main):002:0> ["12", "Hello", "bye"].map {| i | puts i if i.match(/bye/i)}
bye
=> [nil, nil, nil]
irb(main):003:0> ["12", "Hello", "bye"].select {| i | puts i if i.match(/bye/i)}
bye
=> []
Loading

0 comments on commit 608de8e

Please sign in to comment.