Skip to content

Commit cb6a804

Browse files
committed
Add some new ruby stuff
1 parent ad10c88 commit cb6a804

13 files changed

+381
-0
lines changed

Ruby/examples/01-output.rb

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Ruby cheatsheet number 1
2+
# Print stuff in cli
3+
4+
# Print single lines
5+
puts "Hello Jupiter!"
6+
puts 1
7+
puts true
8+
9+
puts "- - - - -"
10+
11+
# Print multi lines
12+
print <<"EOF";
13+
Line one
14+
Second line
15+
EOF
16+
17+
puts "- - - - -"
18+
19+
# Execute codes in cli | One
20+
print <<`EOF`
21+
echo "Hello"
22+
echo "Bye"
23+
EOF
24+
25+
puts "- - - - -"
26+
27+
# Execute codes in cli | Two
28+
print <<`EOF`
29+
ls
30+
ruby --version
31+
EOF
32+
33+
puts "- - - - -"
34+
35+
# Print multi-multi line
36+
37+
# Here we add some parts and in each one we add lines
38+
# We can have 3 or 5 items and 3 or 5 lines!
39+
print <<"One", <<"Two"
40+
Text one & Line one
41+
Text one & Line two
42+
One
43+
Text two & Line one
44+
Text two & Line two
45+
Two

Ruby/examples/02-begin-end.rb

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Ruby cheatsheet number 2
2+
# BEGIN and END statements
3+
4+
# This is the code stuff
5+
6+
print <<"Run"
7+
This is running and stuff
8+
Run
9+
10+
# Begin
11+
# In "begin" we add some codes that are going to run at the first
12+
13+
BEGIN {
14+
puts "Begin part of app!"
15+
puts "- - - - -"
16+
}
17+
18+
# End
19+
# In "End" we add some codes that are going to run at the end
20+
21+
END {
22+
puts "- - - - -"
23+
puts "End part of app!"
24+
}

Ruby/examples/03-classes.rb

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Ruby cheatsheet number 3
2+
# Classes
3+
4+
# Define a global variable
5+
$year = 2022
6+
7+
# Define a class
8+
class Person
9+
# Add initial method
10+
def initialize(name, born)
11+
# Add variables
12+
@name = name
13+
@age = born
14+
end
15+
16+
# Add a sample method
17+
def age
18+
# Return something and using global variable
19+
$year - @age
20+
end
21+
22+
# Create a function with params
23+
def friend(friend)
24+
# Define friend
25+
@friend = friend
26+
27+
# Print with variables
28+
puts "#{@name} is friend with #{@friend}."
29+
end
30+
end
31+
32+
# initial a class with values
33+
amir = Person. new("Amir", 2003)
34+
35+
# using class method and add it to a value
36+
age = amir.age
37+
38+
# Print age
39+
puts age
40+
41+
# Use a class method with params
42+
amir.friend("Fatemeh")

Ruby/examples/04-arrays.rb

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Ruby cheatsheet number 4
2+
# Arrays | 1
3+
4+
# Create just a list
5+
names = [ "Amir", "Fatemeh", "Mohammad", "Ali" ]
6+
7+
# For loop in a list
8+
names.each do | name |
9+
# Print name
10+
puts name
11+
end
12+
13+
puts "- - - - -"
14+
15+
# Create a JSON array
16+
person = {
17+
"name" => "Amir",
18+
"age" => 19,
19+
"born" => "Nov 20 2003",
20+
"city" => "Tehran",
21+
}
22+
23+
# For loop in person with key and value
24+
person.each do |key, value|
25+
# Print key and value
26+
puts "#{key} is #{value}"
27+
end
28+
29+
puts "- - - - -"
30+
31+
(0..10).each do |i|
32+
puts "Running for #{i} time"
33+
end

Ruby/examples/05-operators.rb

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Ruby cheatsheet number 5
2+
# Operators
3+
4+
# =
5+
a = 1
6+
b = 2
7+
c, d, e, f = 3, 4, 5, 6
8+
a, b = c, d
9+
10+
# +
11+
z = a + b + c + d + e + f
12+
puts z
13+
14+
puts "- - - - -"
15+
16+
# defined
17+
# We use this operator to check that variable is defined or not
18+
puts defined? a
19+
puts defined? y
20+
21+
puts "- - - - -"

Ruby/examples/06-conditions.rb

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Ruby cheatsheet number 6
2+
# Conditions
3+
4+
username = "Amir"
5+
password = "hello"
6+
7+
# If, Else
8+
if username == "Amir"
9+
if password == "hello"
10+
puts "User is authenticated"
11+
else
12+
puts "Password is wrong"
13+
end
14+
else
15+
puts "Username is wrong"
16+
end
17+
18+
puts "- - - - -"
19+
20+
age = 19
21+
22+
# Unless
23+
unless age > 20
24+
puts "Age is less than 20"
25+
else
26+
puts "Age is greater than 20"
27+
end
28+
29+
puts "- - - - -"
30+
31+
# Case
32+
action = "login"
33+
case action
34+
when "login"
35+
puts "Login user"
36+
when "logout"
37+
puts "Logout user"
38+
else
39+
puts "Action not found"
40+
end

Ruby/examples/07-loops.rb

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Ruby cheatsheet number 7
2+
# Loops
3+
4+
# While
5+
while_i, while_j = 1, 5
6+
7+
while while_i < while_j do
8+
puts "While is running, i is #{while_i}"
9+
while_i += 1
10+
end
11+
12+
puts "- - - - -"
13+
14+
# Until
15+
until_i, until_j = 1, 5
16+
17+
until until_i > until_j do
18+
puts "Until is running, i is #{until_i}"
19+
until_i += 1
20+
end
21+
22+
puts "- - - - -"
23+
24+
# For
25+
for_n = 5
26+
27+
for n in 0..for_n
28+
puts "For is running, i is #{n}"
29+
end
30+
31+
puts "- - - - -"
32+
33+
# Each
34+
each_array = [ "name", "age", "born", "sruff" ]
35+
36+
each_array.each do |item|
37+
puts "Each is running, i is #{item}"
38+
end
39+
40+
puts "- - - - -"
41+
42+
# Next, Break
43+
for i in 0..5
44+
puts "Number is #{i}"
45+
if i != 4
46+
puts "Number is #{i}. Let's next!"
47+
next
48+
else
49+
puts "Number is #{i}. Let's break!"
50+
break
51+
end
52+
end

Ruby/examples/08-method.rb

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Ruby cheatsheet number 8
2+
# Methods
3+
4+
# Create a sample method
5+
def method1
6+
puts "Method 1 is run"
7+
end
8+
9+
# Create a method with value
10+
def method2(name, age)
11+
puts "#{name} is #{age} years old!"
12+
end
13+
14+
# Return value
15+
def method3(a, b)
16+
a + b
17+
end
18+
19+
# Run methods
20+
method1
21+
22+
puts "- - - - -"
23+
24+
method2("Amir", 19)
25+
26+
puts "- - - - -"
27+
28+
puts method3(10, 20)

Ruby/examples/09-strings.rb

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Ruby cheatsheet number 9
2+
# Strings
3+
4+
name = "amir"
5+
6+
# Downcase
7+
puts name.downcase
8+
9+
# Uppercase
10+
puts name.upcase

Ruby/examples/10-arrays-2.rb

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Ruby cheatsheet number 10
2+
# Arrays | 2
3+
4+
# Create test array
5+
test = Array.new(20)
6+
7+
# Get length and size
8+
puts test.length # 20
9+
puts test.size # 20
10+
11+
# Create array with length and item
12+
test2 = Array.new(10, "Amir")
13+
puts test2
14+
15+
# Create array like a loop
16+
test3 = Array.new(0..10)
17+
puts test3
18+
19+
# Create array with []
20+
test4 = Array.[](1, 2, 3, 4, 5)
21+
test5 = Array[1, 2, 3, 4, 5]

Ruby/examples/11-hash.rb

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Ruby cheatsheet number 11
2+
# Hash
3+
4+
# Create a hash array
5+
a = Hash.new("a")
6+
a = {
7+
"name" => "Amirhossein",
8+
"age" => 19
9+
}
10+
11+
# Print name
12+
puts a["name"]

Ruby/examples/12-datetime.rb

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Ruby cheatsheet number 12
2+
# Date and Time
3+
4+
# New time
5+
time1 = Time.new
6+
puts time1
7+
8+
# Now time. Same as new
9+
time2 = Time.now
10+
puts time2
11+
12+
# Current time
13+
time3 = Time.now
14+
puts time3.inspect
15+
16+
# More about methods
17+
time = Time.new
18+
19+
puts time.year # => Year of the date
20+
puts time.month # => Month of the date (1 to 12)
21+
puts time.day # => Day of the date (1 to 31 )
22+
puts time.wday # => 0: Day of week: 0 is Sunday
23+
puts time.yday # => 365: Day of year
24+
puts time.hour # => 23: 24-hour clock
25+
puts time.min # => 59
26+
puts time.sec # => 59
27+
puts time.usec # => 999999: microseconds
28+
puts time.zone # => "UTC": timezone name
29+
puts time.zone # => "UTC": return the timezone
30+
puts time.utc_offset # => 0: UTC is 0 seconds offset from UTC
31+
puts time.zone # => "PST" (or whatever your timezone is)
32+
puts time.isdst # => false: If UTC does not have DST.
33+
puts time.utc? # => true: if t is in UTC time zone
34+
puts time.localtime # Convert to local timezone.
35+
puts time.gmtime # Convert back to UTC.
36+
puts time.getlocal # Return a new Time object in local zone
37+
puts time.getutc # Return a new Time object in UTC

0 commit comments

Comments
 (0)