Skip to content

Commit b2e29cd

Browse files
author
Darius Neatu
committed
Refactor intro files
1 parent e86c9df commit b2e29cd

10 files changed

+156
-53
lines changed

intro-1.rb

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
a = 10
2-
puts "a string\" #{a}"
3-
puts 'a string #{a}'
4-
p %{a string" " '' ''
5-
foo
6-
bar
7-
}
8-
%(a string)
1+
# base types
2+
p nil
3+
p true
4+
p false
5+
p 1
6+
p 0
97

10-
# => intro-2.rb
8+
# => intro-2.rb

intro-10.rb

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# even more ruby basics
2+
3+
p "a string".capitalize # => "A string"
4+
5+
p 1.to_s # => "1"
6+
7+
p "foo".to_sym # => :foo
8+
9+
p :foo.to_s # => "foo"
10+
11+
# => test/even_more_basics_test.rb
12+
# => intro-11.rb

intro-2.rb

+22-20
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
1-
1 # an integer
2-
# p 1.public_methods
1+
# Truthy and falsey in Ruby
32

4-
MOLOZ = 123
5-
p MOLOZ.to_s
6-
:foo # a symbol
7-
#foo
8-
p :foo.to_s
3+
if true
4+
puts "true is truthy, duh!"
5+
else
6+
puts "true is falsey, wtf!"
7+
end
98

10-
# Strings get allocated each time
11-
p "foo".object_id
12-
p "foo".object_id
13-
p "foo".object_id
9+
if false
10+
puts "false is truthy, wtf!"
11+
else
12+
puts "false is falsey, duh!"
13+
end
1414

15-
# Symbols do not
16-
p :foo.object_id
17-
p :foo.object_id
18-
p :foo.object_id
15+
if nil
16+
puts "nil is truthy"
17+
else
18+
puts "nil is falsey"
19+
end
1920

20-
p 1.to_s.to_sym.public_methods
21+
if 0
22+
puts "0 is truthy"
23+
else
24+
puts "0 is falsey"
25+
end
2126

22-
p "123foo".to_i
23-
p "foo123".to_i
24-
25-
# => intro-21.rb
27+
# => intro-3.rb

intro-3.rb

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
"a string".capitalize # => "A string"
1+
a = 10
22

3-
1.to_s # => "1"
3+
puts "a string \" #{a} \""
4+
puts 'a string #{a}'
45

5-
"foo".to_sym # => :foo
6+
p %{a string" " '' ''
7+
foo
8+
bar
9+
}
10+
%(a string)
611

7-
:foo.to_s # => "foo"
8-
9-
# => test/even_more_basics_test.rb
1012
# => intro-4.rb

intro-4.rb

+10-18
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,14 @@
1-
if true
2-
p :hello
3-
else
4-
p :no_hello
5-
end
1+
# Integers, symbols and strings
62

7-
p :hello if false
3+
1 # an integer
4+
# p 1.public_methods
85

9-
p :hello unless false
6+
MOLOZ = 123
7+
p MOLOZ.to_s
108

11-
bar = 10 if false
9+
:foo # a symbol
10+
#foo
11+
p :foo.to_s
12+
p "foo".to_sym
1213

13-
p bar
14-
15-
a = if true
16-
10
17-
else
18-
11
19-
end
20-
p a
21-
22-
# => methods.rb
14+
# => intro-5.rb

intro-5.rb

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Strings get allocated each time
2+
p "foo".object_id
3+
p "foo".object_id
4+
p "foo".object_id
5+
6+
# Symbols do not
7+
p :foo.object_id
8+
p :foo.object_id
9+
p :foo.object_id
10+
11+
p 1.to_s.to_sym.public_methods
12+
13+
p "123foo".to_i
14+
p "foo123".to_i
15+
16+
# => intro-6.rb

intro-6.rb

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# arrays
2+
# http://ruby-doc.org/core-2.2.0/Array.html
3+
4+
['an', 'array']
5+
[1, 2, 3, 4]
6+
p [1, 2, "foo", :bar]
7+
8+
#%w(an array)
9+
10+
a = %w(foo bar baz) #=> shortcut for array of strings
11+
12+
13+
for value in a
14+
puts value
15+
end
16+
17+
18+
# => test/basics_test.rb
19+
# => intro-7.rb

intro-7.rb

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# arrays - operations
2+
a = [1, 2, 3, 4, 5]
3+
b = [1, 2, 3, 10]
4+
5+
p a + b
6+
p a - b
7+
p a & b
8+
p a | b
9+
p a.include?(1)
10+
p a.include?(10)
11+
12+
a.push(10)
13+
p a
14+
a.pop
15+
p a
16+
17+
a.unshift(10)
18+
p a
19+
p a.shift
20+
p a
21+
22+
# => test/basics_test.rb
23+
# => intro-8.rb

intro-8.rb

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Hash
2+
# http://ruby-doc.org/core-2.2.0/Hash.html
3+
4+
a = { :foo => "bar" } # a hash
5+
6+
a["foo"] = :bar
7+
a[1] = Fixnum
8+
a[Fixnum] = 1
9+
10+
p 1.class
11+
12+
p a.keys
13+
p a.values
14+
15+
p "Pretty display:"
16+
for key, value in a
17+
puts "#{key}(#{key.class}) -> #{value}"
18+
end
19+
20+
# => intro-9.rb

intro-9.rb

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Range
2+
# http://ruby-doc.org/core-2.2.0/Range.html
3+
4+
p (1..10).include?(3) # range
5+
6+
a = (1..3)
7+
8+
for value in a
9+
puts value
10+
end
11+
12+
13+
p (-1..-5).to_a #=> []
14+
p (-5..-1).to_a #=> [-5, -4, -3, -2, -1]
15+
p ('a'..'e').to_a #=> ["a", "b", "c", "d", "e"]
16+
p ('a'...'e').to_a #=> ["a", "b", "c", "d"]
17+
18+
19+
# => intro-10.rb

0 commit comments

Comments
 (0)