Learn to Program(1)

Learn to Program(1)

0. Getting Started
1. Numbers
1.1 Introduction to puts
puts simply writes onto the screen whatever comes after it.

1.2 Integer and Float
numbers without decimal points are called integers, and nunbers with decimal points are usually called floating-point numbers(floats).

1.3 Simple Arithmetic
addition +  subtraction - multiplication * division /

2. Letters
2.1 String Arithmetic
puts 'I like' + 'applie pic.'   #add string

puts 'blink ' * 4               #multiply string
7*3 really just means 7 + 7 + 7, so 'blink ' * 3 just means 'blink ' + 'blink ' + 'blink '

2.2 12 vs '12'
12 is a number, '12' is a string of two digits.

2.3 Problems
puts '12' + 12
puts '2' * '5'

error message: : can't convert Fixnum into String (TypeError)

And 'pig' * 5 is ok, but 5 * 'pig' is silly.

puts 'You're well!'   =====> puts 'You\'re well!'

3. Variables and Assignment
Using vairables
str = '...you can say hello...'
puts str
puts str

We can assign an object to a variable, we can reassgin a different object to that variable.
var = 'just go go go'
puts var
var = 5 * (1 + 2)
puts var

In fact, variables can point to just about anything... except other variables.
var1 = 8
var2 = var1
puts var1
puts var2

var1 = 'eight'
puts var1
puts var2

And the result is:
8 8 eight 8

4. Mixing It Up
4.1 Conversions
Using .to_s to get the string version of an object.
puts 2.to_s + '5'

Using to_i gieves the integer version of an object, to_f gives the float version.
puts 2 + '5'.to_i

puts 99.99.to_i
puts '5 is a number'.to_f
puts 'number 5 is here'.to_i

and the result is 99, 5, 0

4.2 Another Look at Puts
Before puts tries to write out an object, it uses to_s to get the string version of that object. In fact, the s in puts stands for string.

4.3 The Methods gets and chomp
gets just sits there, reading what you type until you press Enter.
puts gets

puts gets.chomp

chomp takes off any Enters hanging out at the end of your string.

5. More About Methods
Every verb needs a noun, so every method needs an object.

If I say puts ' something', what I am really saying is self.puts ' something'.

5.1 Fancy String Methods
string method reverse which gives a backwards version of a string.
var1 = 'stop'
var2 = 'love'

puts var1.reverse
puts var2.reverse

Its results are pots, evol, it is really stange to see the string reverse.

var = 'carl'
puts 'the length of string carl is ' + var.length.to_s

upcase changes every lowercase letter to uppercase.
downcase changes every uppercase letter to lowercase.
swapcase switches the case of every leter in the string
capitalize is just like downcase, except that it switches the first character to uppercase.

var = 'caRl'
puts var.upcase
puts var.downcase
puts var.swapcase
puts var.capitalize

center adds spaces to the beginning and end of the string to make it cenered.
lineWidth = 50
puts('carl is a good guy'.center(lineWidth))

ljust and rjust, which stand for left justify and right justify. They are similar to center.

5.2 Higher Math
% puts 7%3 shows 1

abs, it just takes the absolute value of the number.
puts((-3).abs)

5.3 Random Numbers
The method to get a randomly chosen number is rand.
puts rand
puts rand
puts rand(100)
puts rand(100)

Note that I used rand(101) to get back numbers from 0 to 100, and that rand(1) always gives back 0.
Not understanding the range of possible return values is the biggest mistake.

srand ---> seed and rand

5.4 The Math Object
puts(Math::PI)
puts(Math.sqrt(4))

results: 3.14...., 2.0

references:
http://pine.fm/LearnToProgram/?Chapter=00

你可能感兴趣的:(r)