Skip to content
This repository was archived by the owner on May 23, 2020. It is now read-only.

Commit 386b93c

Browse files
loops complete
1 parent 57174c1 commit 386b93c

File tree

1 file changed

+42
-4
lines changed

1 file changed

+42
-4
lines changed

README.md

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ user_input.gsub!(/s/,"th")
5454
```
5555
This replaces all 's' in the string to 'th'.
5656

57+
_.split_ method returns all words in a string as an array. If text is given as parameter, it will divide the string wherever it sees that bit of text. The test is called _delimiter_.
58+
```ruby
59+
words = text.split(" ")
60+
```
61+
_text_ is a string and _words_ is an array. Space is delimiter here.
62+
5763
## Comments
5864
Single line comments are written with hash(#) in the start. Multi-line comments start with _=begin_ and end with _=end_ .
5965
```ruby
@@ -109,9 +115,9 @@ jobNotDone = false
109115
print 'Good work!!' unless jobNotDone
110116
```
111117

112-
**Relational, Assigment and Comparison Operators** are same as for other common languages: = == > >= < <= !=
113-
**Logical and Boolean operators**: && || !
114-
Complex expressions can be created using these operators: E.g. *(x && (y || w)) && z*
118+
**Relational, Assigment and Comparison Operators** are same as for other common languages: = == > >= < <= != </br>
119+
**Logical and Boolean operators**: && || !</br>
120+
Complex expressions can be created using these operators: E.g. *(x && (y || w)) && z*</br>
115121
Expressions in parentheses are always evaluated before anything outside parentheses.
116122

117123
## Loops and Iterators
@@ -157,4 +163,36 @@ loop do
157163
break if i > 5
158164
end
159165
```
160-
You have to give a condition inside the block which will be used to decide when to break the loop.
166+
A condition is given inside the block which will be used to decide when to break the loop.
167+
The __next__ keyword can be used to skip over certain steps in the loop.
168+
```ruby
169+
i = 20
170+
loop do
171+
i -= 1
172+
next if i%2!=0
173+
puts "#{i}"
174+
break if i <= 0
175+
end
176+
```
177+
This code snippet will print only even numbers from 18 to 0.
178+
179+
Arrays in Ruby are declared as:
180+
```ruby
181+
my_array = [1,2,3,4,5]
182+
```
183+
__.each__ method is more powerful iterator.
184+
```ruby
185+
array = [1,2,3,4,5]
186+
array.each do |x|
187+
x += 10
188+
print "#{x}"
189+
end
190+
```
191+
_do_ and _end_ keywords can be replaced by curly braces {}.
192+
193+
The __.times__ method is like a super compact for loop: it can perform a task on each item in an object a specified number of times.
194+
```ruby
195+
10.times do
196+
print "Hello there!"
197+
end
198+
```

0 commit comments

Comments
 (0)