Debugging in rails Console - ruby

I have this script that i would like to test within the rails console
Gem.find_files("models/*.rb").each do |f|
filename = File.basename(f, '.*')
class_name_symbol = filename.classify.to_sym
autoload class_name_symbol, "models/#{filename}"
end
what i would like to do is print out the results in the console but can only get as far as outputting the array using
Gem.find_files("models/*.rb")
which returns this
["/home/richardlewis/.rvm/gems/ruby-1.9.3-p448#myblogView/bundler/gems/blogModels-8360dfc861ad/lib/models/portfolio_sector.rb", "/home/richardlewis/.rvm/gems/ruby-1.9.3-p448#myblogView/bundler/gems/blogModels-8360dfc861ad/lib/models/post.rb", "/home/richardlewis/.rvm/gems/ruby-1.9.3-p448#myblogView/bundler/gems/blogModels-8360dfc861ad/lib/models/image.rb", "/home/richardlewis/.rvm/gems/ruby-1.9.3-p448#myblogView/bundler/gems/blogModels-8360dfc861ad/lib/models/message.rb", "/home/richardlewis/.rvm/gems/ruby-1.9.3-p448#myblogView/bundler/gems/blogModels-8360dfc861ad/lib/models/sector.rb", "/home/richardlewis/.rvm/gems/ruby-1.9.3-p448#myblogView/bundler/gems/blogModels-8360dfc861ad/lib/models/portfolio.rb", "/home/richardlewis/.rvm/gems/ruby-1.9.3-p448#myblogView/bundler/gems/blogModels-8360dfc861ad/lib/models/category.rb"]
tips appreciated
After using answer the output is
models/portfolio_sector
models/post
models/message
models/sector
models/portfolio
models/category
=> ["/home/richardlewis/.rvm/gems/ruby-1.9.3-p448#myblogView/bundler/gems/blogModels-8360dfc861ad/lib/models/portfolio_sector.rb", "/home/richardlewis/.rvm/gems/ruby-1.9.3-p448#myblogView/bundler/gems/blogModels-8360dfc861ad/lib/models/post.rb", "/home/richardlewis/.rvm/gems/ruby-1.9.3-p448#myblogView/bundler/gems/blogModels-8360dfc861ad/lib/models/image.rb", "/home/richardlewis/.rvm/gems/ruby-1.9.3-p448#myblogView/bundler/gems/blogModels-8360dfc861ad/lib/models/message.rb", "/home/richardlewis/.rvm/gems/ruby-1.9.3-p448#myblogView/bundler/gems/blogModels-8360dfc861ad/lib/models/sector.rb", "/home/richardlewis/.rvm/gems/ruby-1.9.3-p448#myblogView/bundler/gems/blogModels-8360dfc861ad/lib/models/portfolio.rb", "/home/richardlewis/.rvm/gems/ruby-1.9.3-p448#myblogView/bundler/gems/blogModels-8360dfc861ad/lib/models/category.rb"]
not sure why the array is at the end?
Edit :
The script is supposed to take each item in the array and autoload the file contained in models/#{filename}. I would like to print out all the model/#{filename} paths in the console to ensure they are correct –

Gem.find_files("models/*.rb").each do |f|
filename = File.basename(f, '.*')
# So, instead of passing this filename to autoload, you print it. Or do both.
puts "models/#{filename}"
# class_name_symbol = filename.classify.to_sym
# autoload class_name_symbol, "models/#{filename}"
end && nil # suppress return value from `each`

Related

Ruby - iterate tasks with files

I am struggling to iterate tasks with files in Ruby.
(Purpose of the program = every week, I have to save 40 pdf files off the school system containing student scores, then manually compare them to last week's pdfs and update one spreadsheet with every student who has passed their target this week. This is a task for a computer!)
I have converted a pdf file to text, and my program then extracts the correct data from the text files and turns each student into an array [name, score, house group]. It then checks each new array against the data in the csv file, and adds any new results.
My program works on a single pdf file, because I've manually typed in:
f = File.open('output\agb summer report.txt')
agb = []
f.each_line do |line|
agb.push line
end
But I have a whole folder of pdf files that I want to run the program on iteratively. I've also had problems when I try to write each result to a new-named file.
I've tried things with variables and code blocks, but I now don't think you can use a variable in that way?
Dir.foreach('output') do |ea|
f = File.open(ea)
agb = []
f.each_line do |line|
agb.push line
end
end
^ This doesn't work. I've also tried exporting the directory names to an array, and doing something like:
a.each do |ea|
var = '\'output\\' + ea + '\''
f = File.open(var)
agb = []
f.each_line do |line|
agb.push line
end
end
I think I'm fundamentally confused about the sorts of object File and Dir are? I've searched a lot and haven't found a solution yet. I am fairly new to Ruby.
Anyway, I'm sure this can be done - my current backup plan is to copy my program 40 times with different details, but that sounds absurd. Please offer thoughts?
You're very close. Dir.foreach() will return the name of the files whereas File.open() is going to want the path. A crude example to illustrate this:
directory = 'example_directory'
Dir.foreach(directory) do |file|
# Assuming Unix style filesystem, skip . and ..
next if file.start_with? '.'
# Simply puts the contents
path = File.join(directory, file)
puts File.read(path)
end
Use Globbing for File Lists
You need to use Dir#glob to get your list of files. For example, given three PDF files in /tmp/pdf, you collect them with a glob like so:
Dir.glob('/tmp/pdf/*pdf')
# => ["/tmp/pdf/1.pdf", "/tmp/pdf/2.pdf", "/tmp/pdf/3.pdf"]
Dir.glob('/tmp/pdf/*pdf').class
# => Array
Once you have a list of filenames, you can iterate over them with something like:
Dir.glob('/tmp/pdf/*pdf').each do |pdf|
text = %x(pdftotext "#{pdf}")
# do something with your textual data
end
If you're on a Windows system, then you might need a gem like pdf-reader or something else from Ruby Toolbox that suits you better to actually parse the PDF. Regardless, you should use globbing to create a file list; what you do after that depends on what kind of data the file actually holds. IO#read and descendants like File#read are good places to start.
Handling Text Files
If you're dealing with text files rather than PDF files, then something like this will get you started:
Dir.glob('/tmp/pdf/*txt').each do |text|
# Do something with your textual data. In this case, just
# dump the files to standard output.
p File.read(text)
end
You can use Dir.new("./") to get all the files in the current directory
so something like this should work.
file_names = Dir.new "./"
file_names.each do |file_name|
if file_name.end_with? ".txt"
f = File.open(file_name)
agb = []
f.each_line do |line|
agb.push line
end
end
end
btw, you can just use agb = f.to_a to convert the file contents into an array were each element is a line from the file.
file_names = Dir.new "./"
file_names.each do |file_name|
if file_name.end_with? ".txt"
f = File.open file_name
agb = f.to_a
# do whatever processing you need to do
end
end
if you assign your target folder like this /path/to/your/folder/*.txt it will only iterate over text files.
2.2.0 :009 > target_folder = "/home/ziya/Desktop/etc3/example_folder/*.txt"
=> "/home/ziya/Desktop/etc3/example_folder/*.txt"
2.2.0 :010 > Dir[target_folder].each do |texts|
2.2.0 :011 > puts texts
2.2.0 :012?> end
/home/ziya/Desktop/etc3/example_folder/ex4.txt
/home/ziya/Desktop/etc3/example_folder/ex3.txt
/home/ziya/Desktop/etc3/example_folder/ex2.txt
/home/ziya/Desktop/etc3/example_folder/ex1.txt
iteration over text files is ok
2.2.0 :002 > Dir[target_folder].each do |texts|
2.2.0 :003 > File.open(texts, 'w') {|file| file.write("your content\n")}
2.2.0 :004?> end
results
2.2.0 :008 > system ("pwd")
/home/ziya/Desktop/etc3/example_folder
=> true
2.2.0 :009 > system("for f in *.txt; do cat $f; done")
your content
your content
your content
your content

How do I make an array of arrays out of a CSV?

I have a CSV file that looks like this:
Jenny, jenny#example.com ,
Ricky, ricky#example.com ,
Josefina josefina#example.com ,
I'm trying to get this output:
users_array = [
['Jenny', 'jenny#example.com'], ['Ricky', 'ricky#example.com'], ['Josefina', 'josefina#example.com']
]
I've tried this:
users_array = Array.new
file = File.new('csv_file.csv', 'r')
file.each_line("\n") do |row|
puts row + "\n"
columns = row.split(",")
users_array.push columns
puts users_array
end
Unfortunately, in Terminal, this returns:
Jenny
jenny#example.com
Ricky
ricky#example.com
Josefina
josefina#example.com
Which I don't think will work for this:
users_array.each_with_index do |user|
add_page.form_with(:id => 'new_user') do |f|
f.field_with(:id => "user_email").value = user[0]
f.field_with(:id => "user_name").value = user[1]
end.click_button
end
What do I need to change? Or is there a better way to solve this problem?
Ruby's standard library has a CSV class with a similar api to File but contains a number of useful methods for working with tabular data. To get the output you want, all you need to do is this:
require 'csv'
users_array = CSV.read('csv_file.csv')
PS - I think you are getting the output you expected with your file parsing as well, but maybe you're thrown off by how it is printing to the terminal. puts behaves differently with arrays, printing each member object on a new line instead of as a single array. If you want to view it as an array, use puts my_array.inspect.
Assuming that your CSV file actually has a comma between the name and email address on the third line:
require 'csv'
users_array = []
CSV.foreach('csv_file.csv') do |row|
users_array.push row.delete_if(&:nil?).map(&:strip)
end
users_array
# => [["Jenny", "jenny#example.com"],
# ["Ricky", "ricky#example.com"],
# ["Josefina", "josefina#example.com"]]
There may be a simpler way, but what I'm doing there is discarding the nil field created by the trailing comma and stripping the spaces around the email addresses.

Retrieve a file in Ruby

So what I am trying to do is pass a file name into a method and and check if the file is closed. What I am struggling to do is getting a file object from the file name without actually opening the file.
def file_is_closed(file_name)
file = # The method I am looking for
file.closed?
end
I have to fill in the commented part. I tried using the load_file method from the YAML module but I think that gives the content of the file instead of the actual file.
I couldn't find a method in the File module to call. Is there a method maybe that I don't know?
File#closed? returns whether that particular File object is closed, so there is no method that is going to make your current attempted solution work:
f1 = File.new("test.file")
f2 = File.new("test.file")
f1.close
f1.closed? # => true # Even though f2 still has the same file open
It would be best to retain the File object that you're using in order to ask it if it is closed, if possible.
If you really want to know if your current Ruby process has any File objects open for a particular path, something like this feels hack-ish but should mostly work:
def file_is_closed?(file_name)
ObjectSpace.each_object(File) do |f|
if File.absolute_path(f) == File.absolute_path(file_name) && !f.closed?
return false
end
end
true
end
I don't stand by that handling corner cases well, but it seems to work for me in general:
f1 = File.new("test.file")
f2 = File.new("test.file")
file_is_closed?("test.file") # => false
f1.close
file_is_closed?("test.file") # => false
f2.close
file_is_closed?("test.file") # => true
If you want to know if any process has the file open, I think you'll need to resort to something external like lsof.
For those cases where you no longer have access to the original file objects in Ruby (after fork + exec, for instance), a list of open file descriptors is available in /proc/pid/fd. Each file there is named for the file descriptor number, and is a symlink to the opened file, pipe, or socket:
# Returns hash in form fd => filename
def open_file_descriptors
Hash[
Dir.glob( File.join( '/proc', Process.pid.to_s, 'fd', '*' ) ).
map { |fn| [File.basename(fn).to_i, File.readlink(fn)] rescue [nil, nil] }.
delete_if { |fd, fn| fd.nil? or fd < 3 }
]
end
# Return IO object for the named file, or nil if it's not open
def io_for_path(path)
fd, fn = open_file_descriptors.find {|k,v| path === v}
fd.nil? ? nil : IO.for_fd(fd)
end
# close an open file
file = io_for_path('/my/open/file')
file.close unless file.nil?
The open_file_descriptors method parses the fd directory and returns a hash like {3 => '/my/open/file'}. It is then a simple matter to get the file descriptor number for the desired file, and have Ruby produce an IO object for it with for_fd.
This assumes you are on Linux, of course.

Passing in file extension Variable

I am trying to pass in the file extension as a variable but it doesn't seem to be working. when I run getlist.rb(txt)
def getlist(extension)
file = File.new("the_list.txt", "w")
Dir['../path/*.'+ extension].each { |f| file.puts File.basename(f, '.'+ extension).upcase }
end
Basically I want to drop in any file extension (txt, pdf, rb, etc) and it will give me the list without the extension names. Script works fine when it is hard coded just doesn't work when I try to drop in a variable.
Best Regards,
AZCards
My example:
def getlist(extension)
File.open("the_list.txt", "w"){|file|
Dir["*.#{extension}"].each { |f|
file << File.basename(f, File.extname(f)).upcase
file << "\n"
}
}
end
getlist('rb')
getlist(:rb)
My main modifications:
File.open isnted new (you forgot file.close). With File.open and a block the close is done automatic.
Replaces String+ with #-replacements. Advantage: you can use symbols when you call it (you wrote getlist.rb(txt) - this only works, when txt is a variable, but I think you want 'txt')
Use File#extension., see Daves answer.
I added newlines to the resulting file.
Update
Just in case you need a solution with multiple extensions and to call it from command line:
def getlist(*extensions)
p "*.{#{extensions.join(',')}}"
File.open("the_list.txt", "w"){|file|
Dir["*.{#{extensions.join(',')}}"].each { |f|
file << File.basename(f, File.extname(f)).upcase
file << "\n"
}
}
end
#getlist('rb', 'txt')
#getlist(:rb, :txt)
getlist(*ARGV)
Dir['../path/*.'+ extension].each { |f| file.puts File.basename(f, File.extname(f)).upcase }
That said, it works fine for me when using your original string concatenation, too:
ruby-1.9.2-p0 :035 > getlist("html")
=> ["./about.html", "./epl-v10.html", "./notice.html"]
ruby-1.9.2-p0 :037 > getlist("*")
=> ["./libcairo-swt.so", "./eclipse.ini", "./icon.xpm", "./eclipse.ini~", "./about.html", "./epl-v10.html", "./artifacts.xml", "./notice.html"]
ruby-1.9.2-p0 :038 > getlist("ini")
=> ["./eclipse.ini"]
(Prints w/o extension removed for space purposes, but they print w/o the extension, in uppercase.)

Ruby unable to use require

This is a newbie question as I am attempting to learn Ruby by myself, so apologies if it sounds like a silly question!
I am reading through the examples of why's (poignant) guide to ruby and am in chapter 4. I typed the code_words Hash into a file called wordlist.rb
I opened another file and typed the first line as require 'wordlist.rb' and the rest of the code as below
#Get evil idea and swap in code
print "Enter your ideas "
idea = gets
code_words.each do |real, code|
idea.gsub!(real, code)
end
#Save the gibberish to a new file
print "File encoded, please enter a name to save the file"
ideas_name = gets.strip
File::open( 'idea-' + ideas_name + '.txt', 'w' ) do |f|
f << idea
end
When I execute this code, it fails with the following error message:
C:/MyCode/MyRubyCode/filecoder.rb:5: undefined local variable or method `code_words' for main:Object (NameError)
I use Windows XP and Ruby version ruby 1.8.6
I know I should be setting something like a ClassPath, but not sure where/how to do so!
Many thanks in advance!
While the top-level of all files are executed in the same context, each file has its own script context for local variables. In other words, each file has its own set of local variables that can be accessed throughout that file, but not in other files.
On the other hand, constants (CodeWords), globals ($code_words) and methods (def code_words) would be accessible across files.
Some solutions:
CodeWords = {:real => "code"}
$code_words = {:real => "code"}
def code_words
{:real => "code"}
end
An OO solution that is definitely too complex for this case:
# first file
class CodeWords
DEFAULT = {:real => "code"}
attr_reader :words
def initialize(words = nil)
#words = words || DEFAULT
end
end
# second file
print "Enter your ideas "
idea = gets
code_words = CodeWords.new
code_words.words.each do |real, code|
idea.gsub!(real, code)
end
#Save the gibberish to a new file
print "File encoded, please enter a name to save the file"
ideas_name = gets.strip
File::open( 'idea-' + ideas_name + '.txt', 'w' ) do |f|
f << idea
end
I think the problem might be that the require executes the code in another context, so the runtime variable is no longer available after the require.
What you could try is making it a constant:
CodeWords = { :real => 'code' }
That will be available everywhere.
Here is some background on variable scopes etc.
I was just looking at the same example and was having the same problem.
What I did was change the variable name in both files from code_words to $code_words .
This would make it a global variable and thus accesible by both files right?
My question is: wouldn't this be a simpler solution than making it a constant and having to write CodeWords = { :real => 'code' } or is there a reason not to do it ?
A simpler way would be to use the Marshal.dump feature to save the code words.
# Save to File
code_words = {
'starmonkeys' => 'Phil and Pete, those prickly chancellors of the New Reich',
'catapult' => 'chucky go-go', 'firebomb' => 'Heat-Assisted Living',
'Nigeria' => "Ny and Jerry's Dry Cleaning (with Donuts)",
'Put the kabosh on' => 'Put the cable box on'
}
# Serialize
f = File.open('codewords','w')
Marshal.dump(code_words, f)
f.close
Now at the beginning of your file you would put this:
# Load the Serialized Data
code_words = Marshal.load(File.open('codewords','r'))
Here's the easy way to make sure you can always include a file that's in the same directory as your app, put this before the require statement
$:.unshift File.dirname(__FILE__)
$: is the global variable representing the "CLASSPATH"

Resources