Skip to content

Commit

Permalink
Fix markdown in some files for better readability
Browse files Browse the repository at this point in the history
  • Loading branch information
luhova committed Oct 16, 2018
1 parent 737a39e commit 1be50f8
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 61 deletions.
21 changes: 11 additions & 10 deletions Array_in_Ruby.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
Creating an Array
###Creating an Array
Method 1: To create an array in a Ruby program, use square brackets: ([]), and separate the values you want to store with commas.

For example, create an array of sharks and assign it to a variable, like this:

sharks = ["Hammerhead", "Great White", "Tiger"]
```sharks = ["Hammerhead", "Great White", "Tiger"]
print sharks
Output:
["Hammerhead", "Great White", "Tiger"]

["Hammerhead", "Great White", "Tiger"]
```
Method 2: If you want to create an array where each entry is a single word, you can use the %w{} syntax, which creates a word array:

days = %w{Monday Tuesday Wednesday Thursday Friday Saturday Sunday}
`days = %w{Monday Tuesday Wednesday Thursday Friday Saturday Sunday}`

This is equivalent to creating the array with square braces:

days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
`days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]`

However, notice that the %w{} method lets you skip the quotes and the commas.

Arrays are often used to group together lists of similar data types, but in Ruby, arrays can contain any value or a mix of values, including other arrays. Here's an example of an array that contains a string, a nil value, an integer, and an array of strings:

record = [
```record = [
"Sammy",
null,
7,
Expand All @@ -31,16 +31,17 @@ record = [
"array",
]
]

```
Method 3: If you want to create an array of specific size, we have to initialize it with "new" class method.

for eg: names = Array.new(<length of array>)
```for eg: names = Array.new(<length of array>)
Input:
Input:
names = Array.new(20) #length of an Array is 20
puts names.size # This returns 20 (The size)
puts names.length # This also returns 20
Output:
20
20
```
15 changes: 9 additions & 6 deletions Block_in_Ruby.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,33 @@
## Example 1

#!/usr/bin/ruby
def test
```def test
puts "You are in the method"
yield
puts "You are again back to the method"
yield
end
test {puts "You are in the block"}

```
## Example 2
#!/usr/bin/ruby

def test
```def test
yield 5
puts "You are in the method test"
yield 100
end
test {|i| puts "You are in the block #{i}"}

```
## Example 3

def one_two_three
```def one_two_three
yield 1
yield 2
yield 3
end
one_two_three { |number| puts number * 10 }
# 10, 20, 30
10
20
30
```
20 changes: 11 additions & 9 deletions Getting_current_date_and_time.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,33 @@

Following is the simple example to get current date and time:

> time1 = Time.new
```> time1 = Time.new
> puts "Current Time : " + time1.inspect
```

`Time.now` is a synonym:

Time.now is a synonym:

> time2 = Time.now
```> time2 = Time.now
> puts "Current Time : " + time2.inspect
```

This will produce the following result:

```> Current Time : Mon Jun 02 12:02:39 -0700 2008
> Current Time : Mon Jun 02 12:02:39 -0700 2008
> Current Time : Mon Jun 02 12:02:39 -0700 2008
```

We can use Time object to get various components of date and time. Following is the example showing the same:

> time = Time.new

## Components of a Time
```> time = Time.new
> puts "Current Time : " + time.inspect
> puts time.year # => Year of the date
> puts time.year # => Year of the date
> puts time.month # => Month of the date (1 to 12)
Expand All @@ -46,3 +47,4 @@ We can use Time object to get various components of date and time. Following is
> puts time.usec # => 999999: microseconds
> puts time.zone # => "UTC": timezone name
```
6 changes: 3 additions & 3 deletions How to find index of element in array ruby.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#How to find index of element in array ruby

*We have an array of 9 numbers*
a = [9,8,7,6,5,4,3,2,1]
`a = [9,8,7,6,5,4,3,2,1]`

*We put the index value at the variable position, for which we use the a.index method.The index of this value is what we want to get.*
position1 = a.index(8)
`position1 = a.index(8)`

*We show the result*
puts position1;
`puts position1`



31 changes: 16 additions & 15 deletions Loops_in_ruby.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,23 @@ Let's try an example of a loop by creating a file named loop_example.rb

# loop_example.rb

loop do
```loop do
puts "This will keep printing until you hit Ctrl + c"
end
```

Now we can run ruby loop_example.rb on the terminal and see what happens.

You'll notice the same statement keeps printing on the terminal. You'll have to interrupt with a Ctrl + c to stop it.

This will keep printing until you hit Ctrl + c
```This will keep printing until you hit Ctrl + c
This will keep printing until you hit Ctrl + c
This will keep printing until you hit Ctrl + c
This will keep printing until you hit Ctrl + c
This will keep printing until you hit Ctrl + cInterrupt:
from (pry):2:in `puts'
[2] pry(main)>

```
Controlling Loop Execution

You'll hardly do something like this in a real program as it's not very useful and will result in an infinite loop. Eventually your system will crash.
Expand All @@ -35,49 +36,49 @@ Let's look at a more useful example with the break keyword by creating a file na

# useful_loop.rb

i = 0
```i = 0
loop do
i += 1
puts i
break # this will cause execution to exit the loop
end

```
When you run useful_loop.rb in your terminal, the output should be:

$ ruby useful_loop.rb
```$ ruby useful_loop.rb
1

```
The break keyword allows us to exit a loop at any point, so any code after a break will not be executed. Note that break will not exit the program, but only exit the loop and execution will continue on from after the loop.

Next, let's look at adding conditions within a loop by printing all even numbers from 0 up to 10. Let's create a file named conditional_loop.rb

# conditional_loop.rb

i = 0
```i = 0
loop do
i += 2
puts i
if i == 10
break # this will cause execution to exit the loop
end
end

```
Here's the output when we run the file:

$ ruby conditional_loop.rb
```$ ruby conditional_loop.rb
2
4
6
8
10

```
You can see from the above that break was not executed during the first 4 iterations through the loop, but on the 5th iteration, the if statement evaluated to true and so the code within the if statement was executed, which is just break, and execution exited the loop.

We'll talk explicitly about using conditionals within a loop later. Similar to how we use break to exit a loop, we can use the keyword next to skip the rest of the current iteration and start executing the next iteration. We'll use the same example as before to demonstrate. This time we'll skip 4.

# next_loop.rb

i = 0
```i = 0
loop do
i += 2
if i == 4
Expand All @@ -88,15 +89,15 @@ loop do
break
end
end

```
And here's the output when we run the file.

$ ruby next_loop.rb
```$ ruby next_loop.rb
2
6
8
10

```
Notice that the above code did not print out 4, because that was skipped over. Execution continued to the next iteration of the loop.

break and next are important loop control concepts that can be used with loop or any other loop construct in Ruby, which we'll cover one by one below. When combined with conditionals, you can start to build simple programs with interesting behavior.
Expand Down
3 changes: 2 additions & 1 deletion bubble_sort.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
def bubble_sort(list)
```def bubble_sort(list)
return list if list.size <= 1 # already sorted
loop do
swapped = false
Expand All @@ -15,3 +15,4 @@ end
nums = [22,66,77,4,44,457,5,7,24,392,22,8,88,77,33,55,118,99,6,1,62,29,14,139,2,13]
bubble_sort(nums)
```
34 changes: 17 additions & 17 deletions if_else_in_ruby.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,34 @@

The Ruby language has a very simple control structure that is easy to read and follow.

If syntax:
###If syntax:

if var == 10
print “Variable is 10”
```if var == 10
print “Variable is 10”
end
```
###If Else Syntax:

If Else Syntax:

if var == 10
print “Variable is 10”
```if var == 10
print “Variable is 10”
else
print “Variable is something else”
print “Variable is something else”
end

If Else If Syntax:
```
###If Else If Syntax:

Here’s the key difference between Ruby and most other languages. Note that “else if” is actually spelled “elsif” without the e.

if var == 10
print “Variable is 10”
```if var == 10
print “Variable is 10”
elsif var == “20”
print “Variable is 20”
print “Variable is 20”
else
print “Variable is something else”
print “Variable is something else”
end

Ternary (shortened if statement) Syntax:
```
###Ternary (shortened if statement) Syntax:

Ternary syntax is the same in Ruby as most languages. The following sample will print “The variable is 10” if var is equal to 10. Otherwise it will print “The variable is Not 10”.

print “The variable is ” + (var == 10 ? “10” : “Not 10”)
`print “The variable is ” + (var == 10 ? “10” : “Not 10”)`

0 comments on commit 1be50f8

Please sign in to comment.