Skip to content

Commit d112052

Browse files
committed
pushing learn to program examples
1 parent 1176293 commit d112052

14 files changed

Lines changed: 1415 additions & 21 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Learn to Program - Second Edition
2+
# Examples from book
3+
# Sam Gerber
4+
5+
# Chapter 2 - Numbers
6+
7+
puts 1 + 2 # => 3
8+
9+
# 2.4 Simple Arithmetic
10+
puts 1.0 + 2.0 # => 3.0
11+
puts 2.0 * 3.0 # => 6.0
12+
puts 5.0 - 8.0 # => -3.0
13+
puts 9.0 / 2.0 # => 4.5
14+
15+
puts 1 + 2 # => 3
16+
puts 2 * 3 # => 6
17+
puts 5 - 8 # => -3
18+
puts 9 / 2 # => 4
19+
20+
puts 5 * (12-8) + -15 # =>5
21+
puts 98 + (59872 / (13*8)) * -51 # =>-29227

pre-course/learn_to_program/01-chapter_2_numbers/examples.rb

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Learn to Program - Second Edition
2+
# Examples from book
3+
# Sam Gerber
4+
5+
# Chapter 3 - letters
6+
7+
puts 'Hello, world!'# =>Hello, world!
8+
puts '' # =>
9+
puts 'Good-bye.' # =>Good-bye.
10+
11+
# 3.1 String Arithmetic
12+
puts 'I like' + 'apple pie' # =>I likeapple pie
13+
puts 'I like ' + 'apple pie' # =>I like apple pie
14+
puts 'I like' + ' apple pie' # =>I like apple pie
15+
16+
puts 'blink ' * 4 # => blink blink blink blink
17+
18+
# 3.2 12 vs. '12'
19+
puts 12 + 12 # =>24
20+
puts '12' + '12' # =>1212
21+
puts '12 + 12' # =>12 + 12
22+
23+
puts 2 * 5 # =>10
24+
puts '2' * 5 # =>22222
25+
puts '2 * 5' # =>2 * 5
26+
27+
# 3.3 Problems
28+
puts '12' + 12 # => ...no implicit conversion of Fixnum into String
29+
puts '2' * '5' # => ...no implicit conversion of String into Integer
30+
31+
32+
#Escape characters
33+
puts 'You\'re swell!' # =>You're swell!
34+
puts 'backslash at the end of a string: \\' # =>backslash at the end of a string: \
35+
puts 'up\\down' # =>up\down
36+
puts 'up\down' # =>up\down
37+
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Learn to Program - Second Edition
2+
# Examples from book
3+
# Sam Gerber
4+
5+
# Chapter 4 - Variables and Assignment
6+
7+
puts '...you can say that again...' # =>...you can say that again...
8+
puts '...you can say that again...' # =>...you can say that again...
9+
10+
11+
my_string = '...you can say that again...'
12+
puts my_string # =>...you can say that again...
13+
puts my_string # =>...you can say that again...
14+
15+
16+
name = 'Anya Christina Emmanuella Jenkins Harris'
17+
puts 'My name is ' + name + '.'
18+
puts 'Wow! ' + name
19+
puts 'is a really long name!'
20+
21+
# =>My name is Anya Christina Emmanuella Jenkins Harris.
22+
# =>Wow! Anya Christina Emmanuella Jenkins Harris
23+
# =>is a really long name!
24+
25+
26+
composer = 'Mozart'
27+
puts composer + ' was "da bomb" in his day.'
28+
29+
composer = 'Beethoven'
30+
puts 'But I prefer ' + composer + ', personally.'
31+
32+
# =>Mozart was "da bomb" in his day.
33+
# =>But I prefer Beethoven, personally.
34+
35+
36+
my_own_var = 'just another ' + 'string'
37+
puts my_own_var
38+
39+
my_own_var = 5 * (1+2)
40+
puts my_own_var
41+
42+
# =>just another string
43+
# =>15
44+
45+
46+
var1 = 8
47+
var2 = var1
48+
puts var1
49+
puts var2
50+
51+
puts ''
52+
var1 = 'eight'
53+
puts var1
54+
puts var2
55+
56+
# =>8
57+
# =>8
58+
# =>
59+
# =>eight
60+
# =>8
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Learn to Program - Second Edition
2+
# Exercise from Chapter 5 - Mixing It Up
3+
# Sam Gerber
4+
5+
# Write a program that asks for a person's first name, then middle, and then
6+
# last. Finally, it should greet the person using their full name.
7+
8+
def full_name_greeting
9+
puts 'Please type your first name.'
10+
first_name = gets.chomp
11+
puts 'Please type your middle name.'
12+
middle_name = gets.chomp
13+
puts 'Please type your last name.'
14+
last_name = gets.chomp
15+
16+
puts "Hello, #{first_name} #{middle_name} #{last_name}!"
17+
end
18+
19+
full_name_greeting
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Learn to Program - Second Edition
2+
# Exercise from Chapter 5 - Mixing It Up
3+
# Sam Gerber
4+
5+
# Write a program that asks for a person's favorite number.
6+
# Have your program add 1 to the number, and then
7+
# suggest the result as a bigger and better favorite number.
8+
9+
def bigger_better_favorite_number
10+
11+
puts 'Please type your favorite number.'
12+
favorite_number = gets.chomp.to_i
13+
14+
bigger_better_number = favorite_number + 1
15+
16+
puts "Here's a bigger and better favorite number: #{bigger_better_number}!"
17+
end
18+
19+
bigger_better_favorite_number
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Learn to Program - Second Edition
2+
# Examples from book
3+
# Sam Gerber
4+
5+
# Chapter 5 - Mixing It Up
6+
7+
var1 = 2
8+
var2 = '5'
9+
puts var1 + var2
10+
11+
# =>...String can't be coerced into Fixnum
12+
13+
# 5.1 Conversions
14+
15+
var1 = 2
16+
var2 = '5'
17+
puts var1.to_s + var2
18+
19+
# =>25
20+
21+
var1 = 2
22+
var2 = '5'
23+
puts var1.to_s + var2
24+
puts var1 + var2.to_i
25+
26+
# =>25
27+
# =>7
28+
29+
30+
puts '15'.to_f # =>15.0
31+
puts '99.999'.to_f # =>99.999
32+
puts '99.999'.to_i # =>99
33+
puts '' # =>
34+
puts '5 is my favorite number!'.to_i # =>5
35+
puts 'Who asked you about 5...'.to_i # =>0
36+
puts 'Your momma did!'.to_f # =>0.0
37+
puts '' # =>
38+
puts 'stringy'.to_s # =>stringy
39+
puts 3.to_i # =>3
40+
41+
42+
# 5.2 Another Look at puts
43+
44+
puts 20
45+
puts 20.to_s
46+
puts '20'
47+
# =>20
48+
# =>20
49+
# =>20
50+
51+
# 5.3 The gets Method
52+
53+
puts gets
54+
# <=Is there an echo in here?
55+
# =>Is there an echo in here?
56+
57+
# 5.4 Did It Work?
58+
59+
# 5.5 The chomp Method
60+
61+
puts 'Hello there, and what\'s your name\?'
62+
name = gets
63+
puts 'Your name is ' + name + '? What a lovely name!'
64+
puts 'Pleased to meet you, ' + name + '. :)'
65+
# =>Hello there, and what's your name?
66+
# <=Chris
67+
# =>Your name is Chris
68+
# =>? What a lovely name!
69+
# =>Pleased to meet you, Chris
70+
# =>. :)
71+
72+
puts 'Hello there, and what\'s your name\?'
73+
name = gets.chomp
74+
puts 'Your name is ' + name + '? What a lovely name!'
75+
puts 'Pleased to meet you, ' + name + '. :)'
76+
# =>Hello there, and what's your name?
77+
# <=Chris
78+
# =>Your name is Chris? What a lovely name!
79+
# =>Pleased to meet you, Chris. :)
80+
81+
# 5.7 Mind Your Variables
82+
my_birth_month = 'August'
83+
my_birth_day = 3
84+
puts my_birth_month + my_birth_day
85+
# =>...no implicit conversion of Fixnum into String
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Learn to Program - Second Edition
2+
# Exercise from Chapter 6 - More About Methods
3+
# Sam Gerber
4+
5+
# Write an angry boss program that rudely asks what you want.
6+
# Whatever you answer, the angry boss should yell it back to you and then
7+
# fire you. For example, if you type in 'I want a raise', it should yell back
8+
# like this:
9+
# =>WHADDAYA MEAN "I WANT A RAISE"?!? YOU'RE FIRED!!
10+
11+
class Boss
12+
attr_reader :mood
13+
14+
def initialize(name = 'Pointy-Haired Boss')
15+
@name = name
16+
@mood = :angry
17+
end
18+
19+
def manage_employee
20+
puts "What do you want?"
21+
employee_request = gets.chomp
22+
if @mood == :angry
23+
puts "WHADDAYA MEAN \"#{employee_request.upcase}\"?!? YOU'RE FIRED!!"
24+
end
25+
end
26+
end
27+
28+
Boss.new.manage_employee
29+
30+
# =>What do you want?
31+
# <=I would like a raise, sir
32+
# =>WHADDAYA MEAN "I WOULD LIKE A RAISE, SIR?!? YOU'RE FIRED!!
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Learn to Program - Second Edition
2+
# Exercise from Chapter 6 - More About Methods
3+
# Sam Gerber
4+
5+
# Here's something for you to do in order to play around
6+
# more with center, ljust, and rjust: write a program that will display a
7+
# table of contents so that it looks like this:
8+
9+
10+
Chapter = Struct.new(:number, :title, :page_number)
11+
chapters = [Chapter.new(1, "Getting Started", 1),
12+
Chapter.new(2, "Numbers", 9),
13+
Chapter.new(3, "Letters", 13)
14+
]
15+
16+
line_width = 60
17+
18+
puts("Table of Contents".center(line_width))
19+
chapters.each do |chapter|
20+
puts ("Chapter #{chapter.number.to_s.rjust(2)}: #{chapter.title}").ljust(line_width - 7) + ("page#{chapter.page_number.to_s.rjust(3)}")
21+
end
22+
23+
# => Table of Contents
24+
# =>Chapter 1: Getting Started page 1
25+
# =>Chapter 2: Numbers page 9
26+
# =>Chapter 3: Letters page 13

0 commit comments

Comments
 (0)