Skip to content

Instantly share code, notes, and snippets.

@bheeshmar
Created April 11, 2011 22:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bheeshmar/914550 to your computer and use it in GitHub Desktop.
Save bheeshmar/914550 to your computer and use it in GitHub Desktop.
Behavior of different conversion operators provided by Kernel
values = [nil, '', [], {}, true, false, 0, 3.51, '0', '42', '0xb', '1.2', '1e2',
'nil', 'true', 'false' ,'foo', [3,4], {5=>6}]
converters = %w(String to_s Integer to_i Float to_f Array to_a)
table_format = "%9s" * converters.size + "\n"
printf( "%9s ", '')
legend = converters
printf(table_format, *legend)
values.each do |v|
printf "%9s:", v.inspect
results = converters.map do |cvt|
begin
if cvt =~ /to.*/
v.send(cvt).inspect
else
send(cvt, v).inspect
end
rescue => e
e.class.to_s.sub(/Error$/, '')
end
end
printf(table_format, *results)
end
# Results in:
#ruby 1.8.7 (2011-02-18 patchlevel 334) [i386-mingw32]
#
# String to_s Integer to_i Float to_f Array to_a
# nil: "" "" 0 0 Type 0.0 [] []
# "": "" "" Argument 0 Argument 0.0 [] []
# []: "" "" Type NoMethod Type NoMethod [] []
# {}: "" "" Type NoMethod Type NoMethod [] []
# true: "true" "true" Type NoMethod Type NoMethod [true] [true]
# false: "false" "false" Type NoMethod Type NoMethod [false] [false]
# 0: "0" "0" 0 0 0.0 0.0 [0] [0]
# 3.51: "3.51" "3.51" 3 3 3.51 3.51 [3.51] [3.51]
# "0": "0" "0" 0 0 0.0 0.0 ["0"] ["0"]
# "42": "42" "42" 42 42 42.0 42.0 ["42"] ["42"]
# "0xb": "0xb" "0xb" 11 0 Argument 0.0 ["0xb"] ["0xb"]
# "1.2": "1.2" "1.2" Argument 1 1.2 1.2 ["1.2"] ["1.2"]
# "1e2": "1e2" "1e2" Argument 1 100.0 100.0 ["1e2"] ["1e2"]
# "nil": "nil" "nil" Argument 0 Argument 0.0 ["nil"] ["nil"]
# "true": "true" "true" Argument 0 Argument 0.0 ["true"] ["true"]
# "false": "false" "false" Argument 0 Argument 0.0["false"]["false"]
# "foo": "foo" "foo" Argument 0 Argument 0.0 ["foo"] ["foo"]
# [3, 4]: "34" "34" Type NoMethod Type NoMethod [3, 4] [3, 4]
# {5=>6}: "56" "56" Type NoMethod Type NoMethod [[5, 6]] [[5, 6]]
#
#ruby 1.9.2p180 (2011-02-18) [i386-mingw32]
#
# String to_s Integer to_i Float to_f Array to_a
# nil: "" "" Type 0 Type 0.0 [] []
# "": "" "" Argument 0 Argument 0.0 [""] NoMethod
# []: "[]" "[]" Type NoMethod Type NoMethod [] []
# {}: "{}" "{}" Type NoMethod Type NoMethod [] []
# true: "true" "true" Type NoMethod Type NoMethod [true] NoMethod
# false: "false" "false" Type NoMethod Type NoMethod [false] NoMethod
# 0: "0" "0" 0 0 0.0 0.0 [0] NoMethod
# 3.51: "3.51" "3.51" 3 3 3.51 3.51 [3.51] NoMethod
# "0": "0" "0" 0 0 0.0 0.0 ["0"] NoMethod
# "42": "42" "42" 42 42 42.0 42.0 ["42"] NoMethod
# "0xb": "0xb" "0xb" 11 0 11.0 0.0 ["0xb"] NoMethod
# "1.2": "1.2" "1.2" Argument 1 1.2 1.2 ["1.2"] NoMethod
# "1e2": "1e2" "1e2" Argument 1 100.0 100.0 ["1e2"] NoMethod
# "nil": "nil" "nil" Argument 0 Argument 0.0 ["nil"] NoMethod
# "true": "true" "true" Argument 0 Argument 0.0 ["true"] NoMethod
# "false": "false" "false" Argument 0 Argument 0.0["false"] NoMethod
# "foo": "foo" "foo" Argument 0 Argument 0.0 ["foo"] NoMethod
# [3, 4]: "[3, 4]" "[3, 4]" Type NoMethod Type NoMethod [3, 4] [3, 4]
# {5=>6}: "{5=>6}" "{5=>6}" Type NoMethod Type NoMethod [[5, 6]] [[5, 6]]
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment