[ create a new paste ] login | about

Link: http://codepad.org/iML9z94g    [ raw code | output | fork | 1 comment ]

joshua_cheek - Ruby, pasted on Oct 26:
#Learning Ruby chapter 5

#hierarchy and reflection
puts "1 #{1.class}"
puts "2 #{1.class.ancestors.inspect}"
puts "3 #{1.class.included_modules.inspect}"

#base number systems
puts "4 #{ 127       }"  #127 in decimal
puts "5 #{ 0177      }"  #127 in octal
puts "6 #{ 0b1111111 }"  #127 in binary
puts "7 #{ 0x7F      }"  #127 in hexadecimal

#convert to integer
puts "8  #{ Integer(1.4)           }" #from float
puts "9  #{ Integer("256")         }" #from string
puts "10 #{ Integer("0b11110010")  }" #binary from string
puts "11 #{ Integer( 0177 )        }" #from octal
puts "12 #{ Integer( 0x20 )        }" #from hex
puts "13 #{ Integer( ?z )          }" #from character

#division
puts "14 #{(25.0).div(2.0)}"          #divide to integer
puts "15 #{12.2 % 5.0}"               #modulus
puts "16 #{17.divmod(5).inspect}"     #returns array of quotient,remainder
puts "17 #{12.quo(5)}"                #quotient

#bitwise operations
puts "18 #{~-16}"                     #bitwise not
puts "19 #{~16}"                      #bitwise not
puts "20 #{0b1100 | 0b1001}"          #bitwise or
puts "21 #{0b1100 & 0b1001}"          #bitwise and
puts "22 #{0b1100 ^ 0b1001}"          #bitwise xor
puts "23 #{0b1100 ^ 0b0000}"          #bitwise xor (no change)
puts "24 #{0b1100 ^ 0b1111}"          #bitwise xor (bits inverted)
puts "25 #{0b0101 << 1    }"          #shift left  (mult by 2)
puts "25 #{0b1010 >> 1    }"          #shift right (divide by 2)


#"integer?
x = 4
y = 4.1
if x.integer?
  puts "1 #{x}"
else
  puts "1 is not an int"
end

#zero? / nonzero?
x = 0
puts "2 x=0" if x.zero?
x = 1
puts "3 x!=0" if x.nonzero?

#sleep(x) pauses for x seconds

#more math
x = -40
puts "4 x=#{x} x.abs=#{x.abs}"
x = 4.34
puts "5 x=#{x} x.floor=#{x.floor} x.ceil=#{x.ceil}"
puts "6 12.4.round=#{12.4.round} 12.5.round=#{12.5.round} 12.6.round=#{12.6.round}"
puts "7 12.next=#{12.next} -12.next=#{-12.next}"

#constants
puts "8 Math.constants = #{Math.constants.inspect}"
puts "9 Math::PI = #{Math::PI}"
puts "10 Math::E = #{Math::E}"

#exp, sqrt, log, log10
puts "11 Math.exp(5) = #{Math.exp(5)}"
puts "12 Math::E**5 = #{Math::E**5}"
puts "13 Math.sqrt(16) = #{Math.sqrt(16)}"
puts "14 Math.log(Math::E) = #{Math.log(Math::E)}"
puts "15 Math.log(100) = #{Math.log(100)}"
puts "16 Math.log10(100) = #{Math.log10(100)}"
puts "17 Math.log(1) = #{Math.log(1)}"
#puts "18 Math.log(0) = #{Math.log(0)}"

#rational numbers (pg 90)
require 'rational'
require 'mathn'

x = Rational(25/100)
puts "19 25/100 = #{x}"
puts "20 1/4 + 1/4 = #{x+Rational(1/4)}"

#PRIMES!!
prime_number = Prime.new #instantiate a Prime object
print "21 "
10.times{ print prime_number.next , " " }
puts

#number to character based on ascii code (might be removed in Ruby 1.9)
puts "22 97.chr = #{97.chr}"


Output:
1 Fixnum
2 [Fixnum, Integer, Precision, Numeric, Comparable, Object, Kernel]
3 [Precision, Comparable, Kernel]
4 127
5 127
6 127
7 127
8  1
9  256
10 242
11 127
12 32
13 122
14 12
15 2.2
16 [3, 2]
17 2.4
18 15
19 -17
20 13
21 8
22 5
23 12
24 3
25 10
25 5
1 4
2 x=0
3 x!=0
4 x=-40 x.abs=40
5 x=4.34 x.floor=4 x.ceil=5
6 12.4.round=12 12.5.round=13 12.6.round=13
7 12.next=13 -12.next=-11
8 Math.constants = ["PI", "E"]
9 Math::PI = 3.14159265358979
10 Math::E = 2.71828182845905
11 Math.exp(5) = 148.413159102577
12 Math::E**5 = 148.413159102577
13 Math.sqrt(16) = 4.0
14 Math.log(Math::E) = 1.0
15 Math.log(100) = 4.60517018598809
16 Math.log10(100) = 2.0
17 Math.log(1) = 0.0
19 25/100 = 1/4
20 1/4 + 1/4 = 1/2
21 2 3 5 7 11 13 17 19 23 29 
22 97.chr = a


Create a new paste based on this one


Comments:
posted by joshua_cheek on Nov 15
My notes from when I read Learning Ruby
(I didn't expect anyone else to look at them, but a friend asked for info on Ruby)
reply