Skip to content

Commit 1f34e38

Browse files
committed
Init
0 parents  commit 1f34e38

35 files changed

+495
-0
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Book Comprehensive Ruby Programming
2+
3+
Exercises from the book Comprehensive Ruby Programming. Jordan Hudgens
4+

algorithmCountingHumanize.rb

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
require 'humanize'
2+
3+
# p (1..100).to_a.map(&:humanize)
4+
p (1..100).to_a.map(&:humanize).join.tr(" -", "")
5+
p (1..100).to_a.map(&:humanize).join.tr(" -", "").size

algorithmDate.rb

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
require 'date'
2+
3+
start_date = Time.local(1901)
4+
end_date = Time.local(2000,12,31)
5+
sunday_counter = 0
6+
7+
while end_date >= start_date
8+
if end_date.strftime("%A") == "Sunday" && end_date.strftime("%d") == "01"
9+
sunday_counter += 1
10+
end
11+
end_date -= 86400
12+
end
13+
14+
p sunday_counter

algorithmPermutation.rb

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Permutation
2+
3+
p [0,1,2,3,4,5,6,7,8,9].permutation.to_a[999_999].join
4+
5+
6+
arr = [1,2,3]
7+
arr.permutation { |i| p i }
8+
p arr

algorithmPowerDigit.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
p (2 ** 1000).to_s
2+
p (2 ** 1000).to_s.split(//)
3+
p (2 ** 1000).to_s.split(//).map(&:to_i).inject(:+)

algorithms.rb

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
2+
# def bubble_sort(array)
3+
# n = array.length
4+
# loop do
5+
# swapped = false
6+
# (n-1).times do |i|
7+
# if array[i] > array[i+1]
8+
# array[i], array[i+1]=array[i+1], array[i]
9+
# swapped = true
10+
# end
11+
# end
12+
# break if not swapped
13+
# end
14+
# array
15+
# end
16+
17+
# a = [1, 5, 1, 2, 10, 100, 3, 1]
18+
# p bubble_sort(a)
19+
20+
# class Array
21+
# def quicksort
22+
# return [] if empty?
23+
# pivot = delete_at(rand(size))
24+
# left, right = partition(&pivot.method(:>))
25+
# return *left.quicksort, pivot, *right.quicksort
26+
# end
27+
# end
28+
29+
# arr = [34, 2, 1, 5, 3]
30+
# p arr.quicksort
31+
32+
# class Array
33+
# def merge(left, right)
34+
# if left.empty?
35+
# right
36+
# elsif right.empty?
37+
# left
38+
# elsif left.first < right.first
39+
# [left.first] + merge(left[1..left.lenght], right)
40+
# else
41+
# [right.first] + merge(left, right[1..right.lenght])
42+
# end
43+
# end
44+
45+
# def merge_sort(list)
46+
# if list.length <= 1
47+
# list
48+
# else
49+
# mid = (list.lenght / 2).floor
50+
# left = merge_sort(list[0..mid - 1])
51+
# right = merge_sort(list[mid..list.length])
52+
# merge(left, right)
53+
# end
54+
# end
55+
# end
56+
57+
# arr = [4, 1, 5, 1, 33, 312]
58+
# p merge_sort(arr)
59+

algorithmsPrime.rb

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
require 'prime'
2+
3+
prime_array = Prime.take_while{ |p| p < 2_000_000 }
4+
total_count = prime_array.inject { |sum, x| sum + x }
5+
puts total_count
6+
7+
# test 1
8+
arr = [2,3,4,5,6,7,8,9]
9+
puts Prime.first
10+
11+
# test 2
12+
Prime.each(10) do |prime|
13+
p prime
14+
end
15+

apiexemple.rb

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
require 'rubygems'
2+
require 'httparty'
3+
response = HTTParty.get('http://api.stackexchange.com/2.2.questions?site=stackoverflow')
4+
5+
# class EdutechionalResty
6+
# include HTTParty
7+
# base_uri "http://edutechional-resty.herokuapp.com"
8+
9+
# def posts
10+
# self.class.get('/posts.json')
11+
# end
12+
# end
13+
14+
# api = EdutechionalResty.new
15+
# puts api.posts
16+
17+
# class StackExchange
18+
# include HTTParty
19+
# base_uri "http://edutechional-resty.herokuapp.com"
20+
21+
# def initialize(service, page)
22+
# @options = { query: {site: service, page: page}}
23+
# end
24+
25+
# def questions
26+
# self.class.get('/2.2/questions', @options)
27+
# end
28+
29+
# end
30+
31+
class Resty
32+
include HTTParty
33+
base_uri "http://edutechional-resty.herokuapp.com"
34+
35+
def posts
36+
self.class.get('/posts.json')
37+
end
38+
end
39+
40+
# stack_exchange = StackExchange.new('stackoverflow', 1)
41+
# puts stack_exchange.questions
42+
43+
resty = Resty.new
44+
resty_posts = resty.posts
45+
46+
resty_posts.each do |post|
47+
puts "Title: #{post['title']} | Description: #{post['description']}"
48+
end

app.rb

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
require 'sinatra'
2+
3+
class HiSinatra < Sinatra::Base
4+
get '/:age' do
5+
"Hi, I'm #{params[:age]}, years old."
6+
end
7+
end
8+

classes.rb

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Calculator
2+
def self.show
3+
"This is a calculator :D"
4+
end
5+
6+
def summ(x, y)
7+
x + y
8+
end
9+
10+
def subt(x, y)
11+
x - y
12+
end
13+
end
14+
15+
# method without instance object
16+
p Calculator.show
17+
18+
# instancing object
19+
calculator = Calculator.new
20+
21+
# method with object instancied
22+
p calculator.summ(5, 5)

config.rb

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require './app'
2+
run HiSinatra

defaultArguments.rb

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def movie(title:, language: language = "English")
2+
puts title
3+
puts language
4+
end
5+
6+
movie(title: "Robocop", language: "Spanish")

eachLoop.rb

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
array = ["Peter", "Mary", "John", "Konan"]
2+
3+
array.each do |i|
4+
puts "My name is #{i}"
5+
end

fibonacci.rb

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
def fibonacci_digit_counter
2+
num1, num2, i = -1, 0, 1
3+
while i.to_s.length < 1000
4+
num1 += 1
5+
i, num2 = num2, num2 + i
6+
end
7+
num1
8+
end
9+
10+
p fibonacci_digit_counter

files.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
open("files/names.txt", "w") do |f|
2+
f.puts "Hello World!"
3+
end

forLoop.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
for i in 0...10
2+
puts i
3+
end

fortuneGame.rb

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
starting_sentence = "Hi from matching land"
2+
3+
sentence_array = starting_sentence.split("").map(&:downcase)
4+
5+
accurate_count = sentence_array - [" "]
6+
7+
final_sentence = starting_sentence.gsub(/[a-zA-Z]/, "_").split("")
8+
9+
while sentence_array.count("") < accurate_count.count
10+
puts "Guess a letter"
11+
guess = gets.downcase.chomp
12+
if sentence_array.include?(guess)
13+
letter_index = sentence_array.find_index(guess)
14+
sentence_array[letter_index] = ""
15+
final_sentence[letter_index] = guess
16+
puts "Correct! The sentence is now: #{final_sentence.join}"
17+
else
18+
puts "Sorry, that letter isn't the right answer, please try again"
19+
end
20+
end

grep.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
arr = [1, 3, 2, 12, 1, 2, 3]
2+
3+
p arr.grep(1)

hash.rb

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
people = {
2+
:name => "peter",
3+
:age => 19,
4+
:sex => "male"
5+
}
6+
7+
people.each do |key, value|
8+
puts "key: #{key} | value: #{value}"
9+
end

if-else.rb

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
x = 10
2+
y = 5
3+
4+
if x == y
5+
puts "x is the same as y"
6+
else
7+
puts "x isnt the same as y"
8+
end

lambdas.rb

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
hello_lambda = lambda do |msg = "Hi"|
2+
puts msg
3+
end
4+
5+
hello_lambda.call

map.rb

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
array = ["1", "23", "0", "4"]
2+
3+
newArray = array.map do |x|
4+
x.to_i
5+
end
6+
7+
p newArray

metaprogramming.rb

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Baseball
2+
end
3+
4+
class Baseball
5+
def swing
6+
"Homerun"
7+
end
8+
end
9+
10+
p Baseball.new.swing
11+
12+
class Baseball
13+
def swing
14+
"Strike"
15+
end
16+
end
17+
18+
p Baseball.new.swing
19+
20+
21+
22+
class String
23+
def censor(bad_word)
24+
self.gsub! "#{bad_word}", "CENSORED"
25+
end
26+
end
27+
28+
p "The bunny was in trouble with the king's bunny".censor('bunny')
29+
p "The bunny was in trouble with the king's bunny".size
30+
31+
class String
32+
def num_of_chars
33+
size
34+
end
35+
end
36+
37+
p "The bunny was in trouble with the king's bunny".num_of_chars

metaprogramming_define_method.rb

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
require 'ostruct'
2+
3+
# class Author
4+
# define_method("some_method") do
5+
# puts "Some details"
6+
# end
7+
8+
# def fiction_details(arg)
9+
# puts "fiction"
10+
# puts arg
11+
# puts "something else..."
12+
# end
13+
14+
# def coding_details(arg)
15+
# puts "coding"
16+
# puts arg
17+
# puts "something else..."
18+
# end
19+
20+
# def history_details(arg)
21+
# puts "history"
22+
# puts arg
23+
# puts "something else..."
24+
# end
25+
# end
26+
27+
class Author
28+
genres = %w(fiction coding history)
29+
30+
genres.each do |genre|
31+
define_method("#{genre}_details") do |arg|
32+
puts "Genre: #{genre}"
33+
puts arg
34+
puts genre.object_id
35+
end
36+
end
37+
end
38+
39+
author = Author.new
40+
# author.some_method
41+
p 'coding_details-----------------'
42+
author.coding_details "Cal Newport"
43+
p 'fiction_details----------------'
44+
author.fiction_details("Ayn Rand")
45+
p 'respond_to?--------------------'
46+
p author.respond_to?("coding_details")

0 commit comments

Comments
 (0)