0

Sorry for the very basic question.

System information:

  • Language - Ruby
  • Texteditor - Sublime 2
  • Shell - Terminal
  • Operating System - Mac Mavericks

Problem: I am writing code in ruby. How do I view in terminal the results/output from the code? For example, When I run the following code I would like to see how the gsub and squeeze commands modify the test document?

For example:

       require 'tactful_tokenizer'
       require 'treat'           
       require 'pry'            
       require_relative 'lib/extensions/String'

       include Treat::Core::DSL         # Gives quick access to named entity 
       tt = TactfulTokenizer::Model.new # Creates an instance of the tokenizer


       keywordRegexes = [/death/,
                        /died/,
                        /passed \s+ away/xm,]


      #Open example documents

      Dir.glob('examples/*.txt'). each do |filename|
      testdocument = File.open(filename).read


      testdocument.gsub!(/[\n\r]/," ")  
      testdocument.squeeze!(" ")

      end

Thanks in advance

2 Answers 2

1

As you are using pry, you can insert "binding.pry" in the .rb file wherever you want for debugging and run your ruby file. This way you can run your script and dynamically check the values.

Example: I have inserted the binding.pry in the line #5. And debugging the code for some values.

RubyTest username$ ruby timetest.rb "26/11/2014"

From: /Users/username/Languages/Ruby/RubyTest/timetest.rb @ line 5 :

     1: require 'pry'
     2: timenow = (Time.now.utc + 86400).strftime("%d/%m/%Y")
     3: puts timenow.to_s.inspect
     4: 
 =>  5: binding.pry
     6: 
     7: option="24/11/2014 - 30/11/2014"
     8: 
     9: puts option.include? timenow
    10: 

[1] pry(main)> timenow
=> "26/11/2014"
[2] pry(main)> timenow.include?"24/11/2014"
=> false
Sign up to request clarification or add additional context in comments.

Comments

1

After each method you could use the puts method to see the state of the techdocument variable:

  testdocument.gsub!(/[\n\r]/," ")  
  puts testdocument
  testdocument.squeeze!(" ")
  puts testdocument

1 Comment

More tersely you can use tap: testdocument.gsub!(…).tap{ |x| p x }. Guaranteed not to change the return value of your method, unlike an extra puts as the last statement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.