nio 0.2.4 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,9 @@
1
+ == 0.2.5
2
+
3
+ * New features
4
+ - Constructor Fmt() and property setter [].
5
+ - More flexibility for missing parameters for mode, prec.
6
+
1
7
  == 0.2.4 2009-08-05
2
8
 
3
9
  * New features
data/README.txt CHANGED
@@ -305,6 +305,18 @@ we would use, for example:
305
305
  puts 0.1234567.nio_write(fmt.prec(5)) -> 0.12346
306
306
  puts 0.1234567.nio_write(fmt) -> 0.123
307
307
 
308
+ We can use the constructor Fmt() instead of Fmt.default, and pass options to it:
309
+
310
+ fmt = Fmt(:ndig=>3, :dec_sep=>',')
311
+ fmt = Fmt(:ndig=>3) { |f| f.sep! ',' }
312
+
313
+ And the [] operator can be used not only to access predefined formats, but also to
314
+ set individual properties easily, either applied to Fmt or to a format object:
315
+
316
+ fmt = Fmt[:dec_sep=>','].prec(3)[:all_digits=>true]
317
+
318
+ Note that Fmt[...] is simply equivalent to Fmt(...).
319
+
308
320
  ===Exact and aproximate values
309
321
 
310
322
  Float and BigDecimal are approximate in the sense that
@@ -428,7 +428,7 @@ module Nio
428
428
  end
429
429
 
430
430
  @@default_rounding_mode = :even
431
- def initialize()
431
+ def initialize(options=nil)
432
432
 
433
433
  @dec_sep = '.'
434
434
  @grp_sep = ','
@@ -468,6 +468,7 @@ module Nio
468
468
  @nan_txt = 'NAN'
469
469
  @inf_txt = 'Infinity'
470
470
 
471
+ set! options if options
471
472
  yield self if block_given?
472
473
  end
473
474
 
@@ -524,8 +525,8 @@ module Nio
524
525
  # (significance precision) is like :fix but using significant digits
525
526
  # [<tt>:sci</tt>]
526
527
  # (scientific) is the exponential form 1.234E2
527
- # - <tt>precision</tt> (number of digits or :exact, only used for output)
528
- # [<tt>exact</tt>]
528
+ # - <tt>precision</tt> (optional), number of digits or :exact, only used for output
529
+ # [<tt>:exact</tt>]
529
530
  # means that as many digits as necessary to unambiguosly define the
530
531
  # value are used; this is the default.
531
532
  #
@@ -556,22 +557,24 @@ module Nio
556
557
  # zeros of exact types or non-signficative digits of inexact types.
557
558
  # - <tt>:nonsignficative_digits</tt> assigns a character to display
558
559
  # insignificant digits, # by default
559
- def mode(mode,precision=nil,options={})
560
+ def mode(mode, precision=nil, options={})
560
561
  dup.mode!(mode,precision,options)
561
562
  end
562
563
  # This is the mutator version of #mode().
563
- def mode!(mode,precision=nil,options={})
564
+ def mode!(mode, precision=nil, options={})
565
+ precision, options = nil, precision if options.empty? && precision.is_a?(Hash)
564
566
  set! options.merge(:mode=>mode, :ndig=>precision)
565
567
  end
566
568
 
567
569
  # Defines the formatting mode like #mode() but using a different
568
570
  # order of the first two parameters parameters, which is useful
569
571
  # to change the precision only. Refer to #mode().
570
- def prec(precision,mode=nil, options={})
572
+ def prec(precision, mode=nil, options={})
571
573
  dup.prec! precision, mode, options
572
574
  end
573
575
  # This is the mutator version of #prec().
574
- def prec!(precision,mode=:gen, options={})
576
+ def prec!(precision, mode=nil, options={})
577
+ mode, options = nil, mode if options.empty? && mode.is_a?(Hash)
575
578
  set! options.merge(:mode=>mode, :ndig=>precision)
576
579
  end
577
580
 
@@ -1058,13 +1061,18 @@ module Nio
1058
1061
  return num
1059
1062
  end
1060
1063
 
1064
+ def [](options)
1065
+ dup.set! options
1066
+ end
1067
+
1061
1068
 
1062
1069
  @@fmts = {
1063
1070
  :def=>Fmt.new.freeze
1064
1071
  }
1065
1072
  # Returns the current default format.
1066
- def self.default
1073
+ def self.default(options=nil)
1067
1074
  d = self[:def]
1075
+ d = d[options] if options
1068
1076
  if block_given?
1069
1077
  d = d.dup
1070
1078
  yield d
@@ -1079,10 +1087,15 @@ module Nio
1079
1087
  def self.[]=(tag,fmt_def)
1080
1088
  @@fmts[tag.to_sym]=fmt_def.freeze
1081
1089
  end
1082
- # Retrieves a named format from the repository.
1090
+ # Retrieves a named format from the repository or constructs a new
1091
+ # format with the passed options.
1083
1092
  def self.[](tag)
1093
+ if tag.is_a?(Hash)
1094
+ Fmt(tag)
1095
+ else
1084
1096
  @@fmts[tag.to_sym]
1085
1097
  end
1098
+ end
1086
1099
 
1087
1100
  protected
1088
1101
 
@@ -1105,15 +1118,15 @@ module Nio
1105
1118
  @@valid_properties ||= instance_variables.collect{|v| v[1..-1].to_sym}
1106
1119
 
1107
1120
 
1121
+ aliased_properties = {}
1108
1122
  properties.each do |k,v|
1109
1123
  al = ALIAS_PROPERTIES[k]
1110
- if al
1111
- properties[al] = v
1112
- properties.delete k
1113
- elsif !@@valid_properties.include?(k)
1124
+ if al.nil? && !@@valid_properties.include?(k)
1114
1125
  raise InvalidOption, "Invalid option: #{k}"
1115
1126
  end
1127
+ aliased_properties[al || k] = v
1116
1128
  end
1129
+ properties = aliased_properties
1117
1130
 
1118
1131
 
1119
1132
  if properties[:grp_sep].nil? && !properties[:dec_sep].nil? && properties[:dec_sep]!=@dec_sep && properties[:dec_sep]==@grp_sep
@@ -1267,6 +1280,10 @@ module Nio
1267
1280
 
1268
1281
  module_function
1269
1282
 
1283
+ def Fmt(options=nil)
1284
+ Fmt.default(options)
1285
+ end
1286
+
1270
1287
  def nio_float_to_bigdecimal(x,prec) # :nodoc:
1271
1288
  if prec.nil?
1272
1289
  x = Fmt.convert(x,BigDecimal,:approx)
@@ -2,7 +2,7 @@ module Nio #:nodoc:
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
4
  MINOR = 2
5
- TINY = 4
5
+ TINY = 5
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
@@ -1,3 +1,4 @@
1
+ $: << "." unless $:.include?(".") # for Ruby 1.9.2
1
2
  require 'test/unit'
2
3
  require File.dirname(__FILE__) + '/../lib/nio'
3
4
  require File.dirname(__FILE__) + '/../lib/nio/sugar'
@@ -7,7 +7,7 @@
7
7
  # as published by the Free Software Foundation; either version 2
8
8
  # of the License, or (at your option) any later version.
9
9
 
10
- require File.dirname(__FILE__) + '/helper.rb'
10
+ require File.expand_path(File.join(File.dirname(__FILE__),'helper.rb'))
11
11
  require 'test/unit'
12
12
  require 'flt/bigdecimal'
13
13
  include Nio
@@ -73,6 +73,27 @@ class TestFmt < Test::Unit::TestCase
73
73
  assert_equal "123456789.25",(Rational(123456789)+Rational(1,4)).nio_write
74
74
  end
75
75
 
76
+ def test_optional_mode_prec_parameters
77
+ x = 0.1
78
+ assert_equal '0.1000000000', x.nio_write(Fmt.prec(10, :all_digits=>true))
79
+ assert_equal '1.000000000E-1', x.nio_write(Fmt.prec(10, :sci, :all_digits=>true))
80
+ assert_equal '0.1', x.nio_write(Fmt.prec(10, :all_digits=>false))
81
+ assert_equal '0.10000', x.nio_write(Fmt.prec(5).mode(:gen, :all_digits=>true))
82
+ assert_equal '1.0000E-1', x.nio_write(Fmt.prec(5).mode(:sci, :all_digits=>true))
83
+ end
84
+
85
+ def test_fmt_constructor
86
+ x = 0.1
87
+ assert_equal '0.10000000000000001', x.nio_write(Fmt[:all_digits=>true])
88
+ assert_equal '0.1', x.nio_write(Fmt(:all_digits=>false))
89
+ assert_equal '0.10000000000000001', x.nio_write(Fmt(:all_digits=>true))
90
+ assert_equal '0.1', x.nio_write(Fmt.prec(5)[:all_digits=>false])
91
+ assert_equal '0.10000', x.nio_write(Fmt.prec(5)[:all_digits=>true])
92
+ assert_equal '0,1', x.nio_write(Fmt[:comma])
93
+ assert_equal '0.10000000000000001', x.nio_write(Fmt[:all_digits=>true])
94
+ assert_equal '0,10000000000000001', x.nio_write(Fmt[:comma][:all_digits=>true])
95
+ end
96
+
76
97
  def test_basic_fmt_float
77
98
 
78
99
  assert_equal 2,Float::RADIX
@@ -367,7 +388,7 @@ class TestFmt < Test::Unit::TestCase
367
388
  assert_equal Fmt.convert(x_d,Float,:exact), x_f
368
389
  assert_equal Fmt.convert(x_d,Float,:approx), x_f
369
390
 
370
- x_d = BigDecimal('355')/226
391
+ x_d = BigDecimal('355').div(226,20)
371
392
  x_f = Float(355)/226
372
393
  assert_equal Fmt.convert(x_d,Float,:exact), x_f
373
394
  assert_equal Fmt.convert(x_d,Float,:approx), x_f
@@ -7,7 +7,7 @@
7
7
  # as published by the Free Software Foundation; either version 2
8
8
  # of the License, or (at your option) any later version.
9
9
 
10
- #require File.dirname(__FILE__) + '/helper.rb'
10
+ require File.expand_path(File.join(File.dirname(__FILE__),'helper.rb'))
11
11
  require 'test/unit'
12
12
  include Nio
13
13
  require 'yaml'
@@ -1,6 +1,6 @@
1
1
 
2
2
 
3
- require File.dirname(__FILE__) + '/helper.rb'
3
+ require File.expand_path(File.join(File.dirname(__FILE__),'helper.rb'))
4
4
  include Nio
5
5
  require 'yaml'
6
6
  require 'flt'
@@ -12,8 +12,12 @@ require 'bigdecimal/math'
12
12
 
13
13
  require 'bigdecimal/math'
14
14
 
15
- module BgMth
16
- extend BigMath
15
+ if RUBY_VERSION>="1.9.2"
16
+ BgMth = BigMath
17
+ else
18
+ module BgMth
19
+ extend BigMath
20
+ end
17
21
  end
18
22
 
19
23
 
@@ -1,5 +1,5 @@
1
1
 
2
- #require File.dirname(__FILE__) + '/helper.rb'
2
+ require File.expand_path(File.join(File.dirname(__FILE__),'helper.rb'))
3
3
  require 'test/unit'
4
4
 
5
5
  require 'nio/repdec'
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nio
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 2
8
+ - 5
9
+ version: 0.2.5
5
10
  platform: ruby
6
11
  authors:
7
12
  - Javier Goizueta
@@ -9,29 +14,39 @@ autorequire:
9
14
  bindir: bin
10
15
  cert_chain: []
11
16
 
12
- date: 2009-08-05 00:00:00 +02:00
17
+ date: 2010-06-01 00:00:00 +02:00
13
18
  default_executable:
14
19
  dependencies:
15
20
  - !ruby/object:Gem::Dependency
16
21
  name: flt
17
- type: :runtime
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
20
25
  requirements:
21
26
  - - ">="
22
27
  - !ruby/object:Gem::Version
28
+ segments:
29
+ - 1
30
+ - 0
31
+ - 0
23
32
  version: 1.0.0
24
- version:
33
+ type: :runtime
34
+ version_requirements: *id001
25
35
  - !ruby/object:Gem::Dependency
26
36
  name: bones
27
- type: :development
28
- version_requirement:
29
- version_requirements: !ruby/object:Gem::Requirement
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
30
40
  requirements:
31
41
  - - ">="
32
42
  - !ruby/object:Gem::Version
43
+ segments:
44
+ - 2
45
+ - 1
46
+ - 1
33
47
  version: 2.1.1
34
- version:
48
+ type: :development
49
+ version_requirements: *id002
35
50
  description: Numeric input/output
36
51
  email: javier@goizueta.info
37
52
  executables: []
@@ -97,21 +112,25 @@ rdoc_options:
97
112
  require_paths:
98
113
  - lib
99
114
  required_ruby_version: !ruby/object:Gem::Requirement
115
+ none: false
100
116
  requirements:
101
117
  - - ">="
102
118
  - !ruby/object:Gem::Version
119
+ segments:
120
+ - 0
103
121
  version: "0"
104
- version:
105
122
  required_rubygems_version: !ruby/object:Gem::Requirement
123
+ none: false
106
124
  requirements:
107
125
  - - ">="
108
126
  - !ruby/object:Gem::Version
127
+ segments:
128
+ - 0
109
129
  version: "0"
110
- version:
111
130
  requirements: []
112
131
 
113
132
  rubyforge_project: nio
114
- rubygems_version: 1.3.3
133
+ rubygems_version: 1.3.7
115
134
  signing_key:
116
135
  specification_version: 3
117
136
  summary: Numeric input/output