-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Manvendra
committed
Nov 22, 2018
0 parents
commit 608de8e
Showing
165 changed files
with
2,482 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
p | ||
puts | ||
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
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" |
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
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 |
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
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.
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
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) |
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
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. | ||
|
||
|
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
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.
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
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 |
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
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") |
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
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 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
r = File.read('user_input') | ||
puts r.split("\n") |
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
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 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
UN | ||
USA | ||
Bangalore | ||
ABC |
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
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 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
hello bye | ||
world UN |
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
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 |
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
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 |
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
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]") |
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
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 |
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
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/) |
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
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("") |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class A | ||
def p1 | ||
"An instance method" | ||
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
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 |
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
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}" | ||
} |
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
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 | ||
=> [] |
Oops, something went wrong.