Forum: Ruby following along with "Beginning Ruby."

Posted by Al Baker (alcheddah)
on 2012-11-15 19:20
I'm having trouble following along with some of the examples in this
"Beginning Ruby(2nd Edition). I'm at the section where you learn about
I/O handling and tried to do one of the examples and got an error. Not
sure what I did wrong:

irb(main):001:0> File.open("olivertext.txt") do |f|
irb(main):002:1* puts f.gets
irb(main):003:1> end
Errno::ENOENT: No such file or directory - olivertext.txt
        from (irb):1:in `initialize'
        from (irb):1:in `open'
        from (irb):1
        from C:/Ruby193/bin/irb:12:in `<main>'

I tried to use the whole directory to find the file, and got this:

<"C:\Users\Al Baker\Documents\Programming\RuBy olivertext.txt") do |f|
irb(main):005:1* puts f.gets
irb(main):006:1> end
Errno::ENOENT: No such file or directory -
C:UsersAlBakerDocumentsProgrammingRuBy olivertext.txt
        from (irb):4:in `initialize'
        from (irb):4:in `open'
        from (irb):4
        from C:/Ruby193/bin/irb:12:in `<main>'
 help!
Posted by Chris Hulan (Guest)
on 2012-11-15 19:56
(Received via mailing list)
try the second with '/' instead of '\' path separator
Posted by Al Baker (alcheddah)
on 2012-11-15 20:11
Thanks. I wish they could've included that in the book.
 I also had problems using the 'Profile' in irb. I used the code in the 
book:

require 'profile'
class Calculator
  def self.count_to_large_number
    x = 0
    100000.times {x += 1 }
  end

  def self.count_to_small_number
    x = 0
    1000.times {x += 1 }
  end
end
 but only got '100000' and '1000' when I called the methods o for Small 
and large number. smh!
Posted by Joel Pearson (virtuoso)
on 2012-11-15 21:25
I'm not sure profile gem works in IRB. Have you tried running it from an 
.rb file?
Posted by Al Baker (alcheddah)
on 2012-11-15 21:38
I did. I got:

>ruby profiletest.rb
  %   cumulative   self              self     total
 time   seconds   seconds    calls  ms/call  ms/call  name
  0.00     0.00      0.00        1     0.00     0.00  Class#inherited
  0.00     0.00      0.00        2     0.00     0.00
BasicObject#singleton_method_added
  0.00     0.01      0.00        1     0.00    10.00  #toplevel
>Exit code: 0

that's all.

ALSO this code string doesn't really do anything in a text analyzer
script I did.

lines = File.readlines(ARGV[0])
Posted by 7stud -- (7stud)
on 2012-11-16 03:32
Al Baker wrote in post #1084630:
>
> lines = File.readlines(ARGV[0])
>

1) Do you know what ARGV contains?
2) What name did you enter for ARGV[0]
3) What is in the file that has the name given in 2)?
Posted by Al Baker (alcheddah)
on 2012-11-16 04:00
I have no clue. Here's the script so far and how it supposed to work:

stopwords = %w{the a by on for are with just but and to the me I has 
some in}
lines = File.readlines(ARGV[0])
line_count = lines.size
text = lines.join
total_characters = text.length
total_characters_nospaces = text.gsub(/\s+/,'').length
word_count = text.split.length
sentence_count = text.split(/\.|\?|!/).length
paragraph_count = text.split(/\n\n/).length
all_words = text.scan(/\w+/)
good_words = all_words.select{|word| !stopwords.include?(word)}
good_percentage = ((good_words.length.to_f / all_words.length.to_f) 
*100).to_i
sentences = text.gsub(/\s+/, ' ').strip.split(/\.|\?|!/)
sentences_sorted = sentences.sort_by {|sentence| sentence.length}
one_third = sentences_sorted.length/3
ideal_sentences = sentences_sorted.slice(one_third, one_third +1)
ideal_sentneces = ideal_sentences.select {|sentence| sentence =~ 
/is|are/}

puts "#{line_count} lines"
puts "#{total_characters} characters"
puts "#{total_characters_nospaces} characters (excluding spaces)"
puts "#{word_count} words"
# added Vowels in Text.(This part I added my self!!!! :))
puts "#{text.scan(/[aeiou]/).length} vowels in text."
puts "#{sentence_count} sentences"
puts "#{word_count / sentence_count} words per sentence (average)"
puts "#{paragraph_count} paragraph(s)"
puts "#{sentence_count / paragraph_count} sentences per paragraph 
(average)"
puts "#{good_percentage}% of word are non-fluff words"
puts "Summary: \n\n" + ideal_sentences.join(". ")
puts "--End of analysis."
Posted by Al Baker (alcheddah)
on 2012-11-17 06:02
I tried to delete a directory that I created.

Dir.delete("C:/Users/Al Baker/Documents/Programming/RuBy/NewDir")

and got this:

Errno::EACCES: Permission denied - C:/Users/Al 
Baker/Documents/Programming/RuBy/
NewDir
        from (irb):62:in `delete'
        from (irb):62
        from C:/Ruby193/bin/irb:12:in `<main>'
Posted by Joel Pearson (virtuoso)
on 2012-11-17 09:10
Your code works for me when given an argument and a valid text
file. That means the problem is either your argument or your text file.
Posted by Alex Mcmillan (me-wrong)
on 2012-11-17 10:43
(Received via mailing list)
Use FileUtils to delete entire folder/directory - Ruby itself will not 
delete directory if files inside
Posted by Al Baker (alcheddah)
on 2012-11-18 02:50
Alex Mcmillan wrote in post #1084876:
> Use FileUtils to delete entire folder/directory - Ruby itself will not
> delete directory if files inside

what's the method?
Posted by Al Baker (alcheddah)
on 2012-11-18 07:43
nvrmnd. i figured it out. thanks again. :)
Posted by Al Baker (alcheddah)
on 2012-11-20 20:22
I'm havng trouble using mysql in my example.

i alreeady did the gem install and copied the "libmysql.dll" over to bin 
but still got this:

irb(main):005:0> require 'mysql'
LoadError: cannot load such file -- mysql/mysql_api
        from 
C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:i
n `require'
        from 
C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:i
n `require'
        from 
C:/Ruby193/lib/ruby/gems/1.9.1/gems/mysql-2.9.0-x86-mingw32/lib/mys
ql.rb:6:in `rescue in <top (required)>'
        from 
C:/Ruby193/lib/ruby/gems/1.9.1/gems/mysql-2.9.0-x86-mingw32/lib/mys
ql.rb:2:in `<top (required)>'
        from 
C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:i
n `require'
        from 
C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:i
n `require'
        from (irb):5
        from C:/Ruby193/bin/irb:12:in `<main>'

any ideas?
Posted by Joey F. (joey_f)
on 2012-11-22 01:04
Im just a beginer so what is everyone talking about?
Posted by Al Baker (alcheddah)
on 2012-11-22 03:26
Joey F. wrote in post #1085760:
> Im just a beginer so what is everyone talking about?

I'm learning ruby by following along w/ a begnnner's guide to the 
language and some of the examples are a bit troublesome is all.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.