“”.to_i != Integer “”

Posted: September 29, 2006 in RUBY

Baris demi baris ini gw runut dari iTerm gw.. intinya cuman mo nunjukin kenapa kalo “”.to_i itu bisa jadi nol, sedangkan Integer(“”) nggak bisa. Semoga blog yg masi basis wordpress ini bisa nampung semua copy paste gw dgn bener.

Ini buat yang kemaren cerita2 soal gimana ngubah string kosong jadi nol (numerik), jadi ini diangkat dari kasus program nyata loh..

———————————————– Class: Integer < Numeric
Integer is the basis for the two concrete classes that hold whole
numbers, Bignum and Fixnum.
————————————————————————

Includes:
———
Precision(prec, prec_f, prec_i)

Class methods:
————–
induced_from

Instance methods:
—————–
ceil, chr, denominator, downto, floor, gcd, gcd2, gcdlcm,
integer?, lcm, next, numerator, round, succ, times, to_i, to_int,
to_r, truncate, upto
sayang:~ arie$ ri -f bs to_int
More than one method matched your request. You can refine
your search by asking for information on one of:

Float#to_int, Integer#to_int, Numeric#to_int,
Process::Status#to_int, Symbol#to_int
sayang:~ arie$ ri -f bs to_i
More than one method matched your request. You can refine
your search by asking for information on one of:

Float#to_i, Float#to_int, Integer#to_i, Integer#to_int, IO#to_i,
IO#to_io, NilClass#to_i, Numeric#to_int, Process::Status#to_i,
Process::Status#to_int, Rational#to_i, String#to_i, Symbol#to_i,
Symbol#to_int, Time#to_i, Zlib::GzipFile#to_io
sayang:~ arie$ ri -f bs String#to_i
———————————————————— String#to_i
str.to_i(base=10) => integer
————————————————————————
Returns the result of interpreting leading characters in str as an
integer base base (2, 8, 10, or 16). Extraneous characters past
the end of a valid number are ignored. If there is not a valid
number at the start of str, 0 is returned. This method never
raises an exception.

“12345”.to_i #=> 12345
“99 red balloons”.to_i #=> 99
“0a”.to_i #=> 0
“0a”.to_i(16) #=> 10
“hello”.to_i #=> 0
“1100101”.to_i(2) #=> 101
“1100101”.to_i(8) #=> 294977
“1100101”.to_i(10) #=> 1100101
“1100101”.to_i(16) #=> 17826049

sayang:~ arie$ irb
irb(main):001:0> ”.to_i
=> 0
irb(main):002:0> Integer(”)
ArgumentError: invalid value for Integer: “”
from (irb):2:in `Integer’
from (irb):2
irb(main):003:0> Integer(10.02)
=> 10
irb(main):004:0>

# komen ariekeren buat yang geblek bin rewel ngerecokin: jangan banyak tereak2/permintaan deh ni arie baru abis sakit tergeletak aja sendiri demam panas batuk pilek sejak jumat kmaren. Arie had died several times, then he was reborn again and again kek phoenix # alah, koq jadi inget cerita Harry Potter yaa …

bahkan..

irb(main):004:0> nil
=> nil
irb(main):005:0> nil.class
=> NilClass
irb(main):006:0> nil.to_s
=> “”
irb(main):007:0> nil.to_s.size
=> 0
irb(main):008:0> nil.to_s.to_i
=> 0
irb(main):009:0> nil.object_id
=> 4
irb(main):010:0> nil.to_i
=> 0
irb(main):011:0> nil.to_i.class
=> Fixnum
irb(main):012:0>

;-)

tinggalkan feedback konstruktif atau lebih baik diam

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s