Skip to content

Commit 0da909d

Browse files
authored
Ruby : Crazy Syntax (#244)
* Create CrazySyntax.rb * Create README.md * Update /README.md
1 parent a9f54e8 commit 0da909d

File tree

3 files changed

+173
-0
lines changed

3 files changed

+173
-0
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ miscellaneous petty works
1414
- [My **C#** Practice](#my-c-practice)
1515
- [My **Java** Practice](#my-java-practice)
1616
- [My **Golang** Practice](#my-golang-practice)
17+
- [My **Ruby** Practice](#my-ruby-practice)
1718
- [My **BASIC** Practice](#my-basic-practice)
1819
- [My **Verilog** Practice](#my-verilog-practice)
1920
- [My **Auto Hot Key** Practice](#my-auto-hot-key-practice)
@@ -124,6 +125,11 @@ making a dashboard for managing Gitgub contributions by using `Github API`
124125
- [Grade (2021.12.06)](/Golang#grade-20211206)
125126

126127

128+
## [My Ruby Practice](./Ruby/)
129+
130+
- [Ruby's Crazy Syntax (2022.12.24)](./Ruby/README.md#rubys-crazy-syntax-20221224)
131+
132+
127133
## [My BASIC Practice](/BASIC#my-qbasic-practice)
128134

129135
- [Draw A Car (2022.02.09)](/BASIC#draw-a-car-20220209)

Ruby/CrazySyntax.rb

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Ruby's crazy syntax
2+
3+
# 2022.12.24 (It is a good day to code!)
4+
5+
6+
# Falsey 0
7+
8+
if 0 == false
9+
puts "0 is false."
10+
elsif 0 == true
11+
puts "0 is true, surprisingly."
12+
else
13+
puts "0 is neither false nor true but falsey."
14+
end
15+
16+
17+
# Comparison operator vs eql?
18+
19+
puts 1 == 1.0 # true
20+
puts 1.eql?(1.0) # false
21+
22+
23+
# Range : .. vs ...
24+
25+
rng1 = (1..10).to_a # 1 ~ 10
26+
rng2 = (1...10).to_a # 1 ~ 9
27+
28+
print rng1, "\n"
29+
print rng2, "\n"
30+
31+
32+
# Array : reverse vs reverse!
33+
34+
arr = [1, 2, 3]
35+
36+
arr.reverse # not save
37+
print arr, "\n"
38+
39+
arr.reverse! # save
40+
print arr, "\n"
41+
42+
43+
# Symbol : similar with strings but immutable and faster
44+
45+
str = "I"
46+
sym = :U
47+
str += "♥"
48+
# sym += "2" # undefined method `+' for :U:Symbol (NoMethodError)
49+
print str
50+
print sym, "\n"
51+
52+
53+
# .times : an iterator for numbers
54+
55+
10.times do
56+
print "no"
57+
end
58+
print "\n"
59+
60+
61+
# Return : return the last line's evaluated result without return in UDF
62+
63+
def Say()
64+
a = "I hate you"
65+
b = "You make me crazy"
66+
c = "I want to leave you, but"
67+
d = "I love you"
68+
end
69+
70+
puts Say()

Ruby/README.md

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# [My Ruby Practice](../README.md#my-ruby-practice)
2+
3+
Like a red ruby, she told me with confidence that she loved you more than I did
4+
5+
6+
## \<List>
7+
8+
- [Ruby's Crazy Syntax (2022.12.24)](#rubys-crazy-syntax-20221224)
9+
10+
11+
## [Ruby's Crazy Syntax (2022.12.24)](#list)
12+
13+
#### `CrazySyntax.rb`
14+
15+
```ruby
16+
# Falsey 0
17+
18+
if 0 == false
19+
puts "0 is false."
20+
elsif 0 == true
21+
puts "0 is true, surprisingly."
22+
else
23+
puts "0 is neither false nor true but falsey."
24+
end
25+
```
26+
> 0 is neither false nor true but falsey.
27+
28+
```ruby
29+
# Comparison operator vs eql?
30+
31+
puts 1 == 1.0 # true
32+
puts 1.eql?(1.0) # false
33+
```
34+
> true
35+
> false
36+
37+
```ruby
38+
# Range : .. vs ...
39+
40+
rng1 = (1..10).to_a # 1 ~ 10
41+
rng2 = (1...10).to_a # 1 ~ 9
42+
43+
print rng1, "\n"
44+
print rng2, "\n"
45+
```
46+
> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
47+
> [1, 2, 3, 4, 5, 6, 7, 8, 9]
48+
49+
```ruby
50+
# Array : reverse vs reverse!
51+
52+
arr = [1, 2, 3]
53+
54+
arr.reverse # not save
55+
print arr, "\n"
56+
57+
arr.reverse! # save
58+
print arr, "\n"
59+
```
60+
> [1, 2, 3]
61+
> [3, 2, 1]
62+
63+
```ruby
64+
# Symbol : similar with strings but immutable and faster
65+
66+
str = "I"
67+
sym = :U
68+
str += ""
69+
# sym += "2" # undefined method `+' for :U:Symbol (NoMethodError)
70+
print str
71+
print sym, "\n"
72+
```
73+
> I♥U
74+
75+
```ruby
76+
# .times : an iterator for numbers
77+
78+
10.times do
79+
print "no"
80+
end
81+
print "\n"
82+
```
83+
> nononononononononono
84+
85+
```ruby
86+
# Return : return the last line's evaluated result without return in UDF
87+
88+
def Say()
89+
a = "I hate you"
90+
b = "You make me crazy"
91+
c = "I want to leave you, but"
92+
d = "I love you"
93+
end
94+
95+
puts Say()
96+
```
97+
> I love you

0 commit comments

Comments
 (0)