5-1-5 Integer

In [2]:
p 100.class
p 10000000000000000000000000000000000.class
Fixnum
Bignum
Out[2]:
Bignum
In [8]:
60.step(70, 1).each{ |i| puts "%i\t%s"%[i, (2 ** i).class]}
60	Fixnum
61	Fixnum
62	Bignum
63	Bignum
64	Bignum
65	Bignum
66	Bignum
67	Bignum
68	Bignum
69	Bignum
70	Bignum
Out[8]:
60
In [9]:
1.odd?
2.even?
Out[9]:
true
In [11]:
p (0..10).to_a.select(&:odd?)
p (0..10).to_a.select(&:even?)
[1, 3, 5, 7, 9]
[0, 2, 4, 6, 8, 10]
Out[11]:
[0, 2, 4, 6, 8, 10]
In [13]:
p 2.next
p 2.succ
p 2.pred
3
3
1
Out[13]:
1
In [15]:
1 / 0
ZeroDivisionError: divided by 0
(pry):20:in `/'
(pry):20:in `<main>'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:355:in `eval'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:355:in `evaluate_ruby'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:323:in `handle_line'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:243:in `block (2 levels) in eval'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:242:in `catch'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:242:in `block in eval'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:241:in `catch'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:241:in `eval'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/iruby-0.2.7/lib/iruby/backend.rb:65:in `eval'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/iruby-0.2.7/lib/iruby/backend.rb:12:in `eval'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/iruby-0.2.7/lib/iruby/kernel.rb:87:in `execute_request'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/iruby-0.2.7/lib/iruby/kernel.rb:47:in `dispatch'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/iruby-0.2.7/lib/iruby/kernel.rb:37:in `run'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/iruby-0.2.7/lib/iruby/command.rb:70:in `run_kernel'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/iruby-0.2.7/lib/iruby/command.rb:34:in `run'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/iruby-0.2.7/bin/iruby:5:in `<top (required)>'
/Users/mic/.rbenv/versions/2.2.2/bin/iruby:23:in `load'
/Users/mic/.rbenv/versions/2.2.2/bin/iruby:23:in `<main>'
In [16]:
p 30.to_s
p 30.to_s(2)
p 30.to_s(8)
p 30.to_s(16)
"30"
"11110"
"36"
"1e"
Out[16]:
"1e"
In [17]:
(65..70).map {|n| n.chr}
Out[17]:
["A", "B", "C", "D", "E", "F"]
In [18]:
256.chr
RangeError: 256 out of char range
(pry):26:in `chr'
(pry):26:in `<main>'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:355:in `eval'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:355:in `evaluate_ruby'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:323:in `handle_line'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:243:in `block (2 levels) in eval'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:242:in `catch'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:242:in `block in eval'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:241:in `catch'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:241:in `eval'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/iruby-0.2.7/lib/iruby/backend.rb:65:in `eval'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/iruby-0.2.7/lib/iruby/backend.rb:12:in `eval'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/iruby-0.2.7/lib/iruby/kernel.rb:87:in `execute_request'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/iruby-0.2.7/lib/iruby/kernel.rb:47:in `dispatch'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/iruby-0.2.7/lib/iruby/kernel.rb:37:in `run'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/iruby-0.2.7/lib/iruby/command.rb:70:in `run_kernel'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/iruby-0.2.7/lib/iruby/command.rb:34:in `run'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/iruby-0.2.7/bin/iruby:5:in `<top (required)>'
/Users/mic/.rbenv/versions/2.2.2/bin/iruby:23:in `load'
/Users/mic/.rbenv/versions/2.2.2/bin/iruby:23:in `<main>'
In [19]:
"あ".ord
Out[19]:
12354
In [27]:
[12354, 12353, 12356].pack("U*")
Out[27]:
"あぁい"
In [28]:
"あいうえ".unpack("U*")
Out[28]:
[12354, 12356, 12358, 12360]
In [30]:
"A".ord
Out[30]:
65
In [31]:
12354.chr("UTF-8")
Out[31]:
"あ"
In [32]:
p '123'.to_i
p Time.now.to_i
123
1456312066
Out[32]:
1456312066
In [33]:
Integer('234')
Out[33]:
234
In [34]:
Integer(nil)
TypeError: can't convert nil into Integer
(pry):43:in `Integer'
(pry):43:in `<main>'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:355:in `eval'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:355:in `evaluate_ruby'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:323:in `handle_line'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:243:in `block (2 levels) in eval'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:242:in `catch'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:242:in `block in eval'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:241:in `catch'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:241:in `eval'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/iruby-0.2.7/lib/iruby/backend.rb:65:in `eval'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/iruby-0.2.7/lib/iruby/backend.rb:12:in `eval'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/iruby-0.2.7/lib/iruby/kernel.rb:87:in `execute_request'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/iruby-0.2.7/lib/iruby/kernel.rb:47:in `dispatch'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/iruby-0.2.7/lib/iruby/kernel.rb:37:in `run'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/iruby-0.2.7/lib/iruby/command.rb:70:in `run_kernel'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/iruby-0.2.7/lib/iruby/command.rb:34:in `run'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/iruby-0.2.7/bin/iruby:5:in `<top (required)>'
/Users/mic/.rbenv/versions/2.2.2/bin/iruby:23:in `load'
/Users/mic/.rbenv/versions/2.2.2/bin/iruby:23:in `<main>'
In [37]:
Integer '123'
Out[37]:
123
In [38]:
Array(1)
Out[38]:
[1]
In [41]:
Array([1, 2])
Out[41]:
[1, 2]
In [42]:
Array(nil)
Out[42]:
[]
In [43]:
Integer('abc')
ArgumentError: invalid value for Integer(): "abc"
(pry):52:in `Integer'
(pry):52:in `<main>'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:355:in `eval'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:355:in `evaluate_ruby'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:323:in `handle_line'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:243:in `block (2 levels) in eval'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:242:in `catch'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:242:in `block in eval'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:241:in `catch'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:241:in `eval'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/iruby-0.2.7/lib/iruby/backend.rb:65:in `eval'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/iruby-0.2.7/lib/iruby/backend.rb:12:in `eval'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/iruby-0.2.7/lib/iruby/kernel.rb:87:in `execute_request'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/iruby-0.2.7/lib/iruby/kernel.rb:47:in `dispatch'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/iruby-0.2.7/lib/iruby/kernel.rb:37:in `run'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/iruby-0.2.7/lib/iruby/command.rb:70:in `run_kernel'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/iruby-0.2.7/lib/iruby/command.rb:34:in `run'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/iruby-0.2.7/bin/iruby:5:in `<top (required)>'
/Users/mic/.rbenv/versions/2.2.2/bin/iruby:23:in `load'
/Users/mic/.rbenv/versions/2.2.2/bin/iruby:23:in `<main>'
In [44]:
Integer([1])
TypeError: can't convert Array into Integer
(pry):53:in `Integer'
(pry):53:in `<main>'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:355:in `eval'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:355:in `evaluate_ruby'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:323:in `handle_line'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:243:in `block (2 levels) in eval'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:242:in `catch'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:242:in `block in eval'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:241:in `catch'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:241:in `eval'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/iruby-0.2.7/lib/iruby/backend.rb:65:in `eval'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/iruby-0.2.7/lib/iruby/backend.rb:12:in `eval'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/iruby-0.2.7/lib/iruby/kernel.rb:87:in `execute_request'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/iruby-0.2.7/lib/iruby/kernel.rb:47:in `dispatch'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/iruby-0.2.7/lib/iruby/kernel.rb:37:in `run'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/iruby-0.2.7/lib/iruby/command.rb:70:in `run_kernel'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/iruby-0.2.7/lib/iruby/command.rb:34:in `run'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/iruby-0.2.7/bin/iruby:5:in `<top (required)>'
/Users/mic/.rbenv/versions/2.2.2/bin/iruby:23:in `load'
/Users/mic/.rbenv/versions/2.2.2/bin/iruby:23:in `<main>'
In [46]:
1.upto 3 do |num|
  puts num
end

puts '-----------------'

3.downto 1 do |num|
  puts num
end
1
2
3
-----------------
3
2
1
Out[46]:
3
In [47]:
3.times do |num|
  puts num
end
0
1
2
Out[47]:
3
In [48]:
3.times.class
Out[48]:
Enumerator

5-1-6 Float

In [55]:
sum = 0.0; 10.times{|i| sum += 0.1 }; p sum
0.9999999999999999
Out[55]:
0.9999999999999999
In [58]:
p 1.0 / 0 
p -1.0 / 0
p 0.0 / 0
Infinity
-Infinity
NaN
Out[58]:
NaN

5-1-7 Rational

In [59]:
r = Rational(1, 3)
Out[59]:
(1/3)
In [60]:
p r.denominator
p r.numerator
3
1
Out[60]:
1
In [66]:
sum = 0; 10.times{|i| sum += 1/10r }; p sum
(1/1)
Out[66]:
(1/1)
In [67]:
3.quo(10)
Out[67]:
(3/10)
In [68]:
Time.now.to_r
Out[68]:
(1456312849490761/1000000)
In [69]:
Complex(2, 3)
Out[69]:
$2+3\imath$
In [70]:
Complex('2+2i')
Out[70]:
$2+2\imath$
In [ ]: