Ejercicios: Expresiones Regulares. Un programa que convierte de Celsius a Farenheit

Ejercicio 8.7.1   El siguiente programa convierte de Celsius a Farenheit y viceversa:

MacBookdeCasiano:rubytesting casiano$ ruby f2c.rb 32F 
celsius = 0.0 farenheit = 32.0
Ejecútelo con y sin la opción -d
[~/rubytesting/f2c]$ ruby -d f2c.rb 32F
Exception `LoadError' at /Users/casiano/.rvm/rubies/ruby-1.9.3-p545/lib/ruby/site_ruby/1.9.1/rubygems.rb:1194 - cannot load such file -- rubygems/defaults/operating_system
Exception `LoadError' at /Users/casiano/.rvm/rubies/ruby-1.9.3-p545/lib/ruby/site_ruby/1.9.1/rubygems.rb:1203 - cannot load such file -- rubygems/defaults/ruby
32F
Matches num = 32 kind= F
celsius = 0.0 farenheit = 32.0
[~/rubytesting/f2c]$ ruby -d f2c.rb 32X
Exception `LoadError' at /Users/casiano/.rvm/rubies/ruby-1.9.3-p545/lib/ruby/site_ruby/1.9.1/rubygems.rb:1194 - cannot load such file -- rubygems/defaults/operating_system
Exception `LoadError' at /Users/casiano/.rvm/rubies/ruby-1.9.3-p545/lib/ruby/site_ruby/1.9.1/rubygems.rb:1203 - cannot load such file -- rubygems/defaults/ruby
32X
does not match
[~/rubytesting/f2c]$ ruby -v
ruby 1.9.3p545 (2014-02-24 revision 45159) [x86_64-darwin13.1.0]
e intente averiguar el significado de cada una de las sentencias.

$ cat -n f2c.rb 
     1  #!/usr/bin/env ruby
     2  
     3  t = ARGV[0] || '0C'
     4  puts t if $DEBUG
     5  
     6  if t =~ /^\s*(\d+(?:\.?\d+)?)([CF])\s*$/i
     7    puts "Matches num = #{$1} kind= #{$2}" if $DEBUG
     8    num, type = $1.to_f, $2
     9  
    10    if (type == 'C') 
    11      celsius = num
    12      farenheit = (celsius*9/5)+32
    13    else
    14      farenheit = num
    15      celsius = (farenheit-32)*5/9
    16    end
    17    puts "celsius = #{celsius} farenheit = #{farenheit}"
    18  else
    19    puts "does not match" if $DEBUG
    20  end
para entenderlo un poco consulte las siguientes secciones de la guía de usuario:

Debugging con pry

Pry can be invoked in the middle of a running program. It opens a Pry session at the point it's called and makes all program state at that point available.

It can be invoked on any object using the my_object.pry syntax or on the current binding (or any binding) using binding.pry.

The Pry session will then begin within the scope of the object (or binding).

When the session ends the program continues with any modifications you made to it.

[~/rubytesting/f2c]$ cat f2c2.rb 
#!/usr/bin/env ruby
require 'pry'

t = ARGV[0] || '0C'

if t =~ /^\s*(\d+(?:\.?\d+)?)([CF])\s*$/i
  num, type = $1.to_f, $2

  binding.pry
  if (type == 'C') 
    celsius = num
    farenheit = (celsius*9/5)+32
  else
    farenheit = num
    celsius = (farenheit-32)*5/9
  end
  puts "celsius = #{celsius} farenheit = #{farenheit}"
else
  $stderr.puts "not expected input"
end

[~/rubytesting/f2c]$ cat Gemfile
source 'https://rubygems.org'

gem 'pry'
gem 'pry-debugger' if RUBY_VERSION == "1.9.3"
gem 'pry-byebug' if RUBY_VERSION == "2.1.2"

[~/rubytesting/f2c]$ ruby -v
ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-darwin13.0]
[~/rubytesting/f2c]$ bundle
Using columnize 0.8.9
Using debugger-linecache 1.2.0
Using slop 3.6.0
Using byebug 3.5.1
Using coderay 1.1.0
Using method_source 0.8.2
Using pry 0.10.1
Using pry-byebug 2.0.0
Using bundler 1.6.2
Your bundle is complete!
Use `bundle show [gemname]` to see where a bundled gem is installed.

[~/rubytesting/f2c]$ ruby -v
ruby 1.9.3p545 (2014-02-24 revision 45159) [x86_64-darwin13.1.0]
[~/rubytesting/f2c]$ bundle
Resolving dependencies...
Using coderay 1.1.0
Using columnize 0.8.9
Using debugger-linecache 1.2.0
Using debugger-ruby_core_source 1.3.5
Using debugger 1.6.8
Using method_source 0.8.2
Using slop 3.6.0
Using pry 0.10.1
Using pry-debugger 0.2.3
Using bundler 1.6.0
Your bundle is complete!
Use `bundle show [gemname]` to see where a bundled gem is installed.

[~/rubytesting/f2c]$ ruby f2c2.rb 

From: /Users/casiano/local/src/ruby/rubytesting/f2c/f2c2.rb @ line 10 :

     5: 
     6: if t =~ /^\s*(\d+(?:\.?\d+)?)([CF])\s*$/i
     7:   num, type = $1.to_f, $2
     8: 
     9:   binding.pry
 => 10:   if (type == 'C') 
    11:     celsius = num
    12:     farenheit = (celsius*9/5)+32
    13:   else
    14:     farenheit = num
    15:     celsius = (farenheit-32)*5/9

[1] pry(main)> p num
0.0
=> 0.0
[2] pry(main)> continue
celsius = 0.0 farenheit = 32.0



Subsecciones
Casiano Rodriguez León 2015-01-07