I have a ruby code that supposes to read a paragraph and find the total number of characters, words, and sentences then find the ARI (Automated Readability Index) and decide which grade level.
I have the code here but when running it I'm getting these two errors, this is my first time using ruby so I'm not so familiar with its error.
here's the code
def calcARI(paragraph)
file = File.open(paragraph,"r")
if file
file_data = file.read
file.close
charCount=0
wordCount=0
sentenceCount=0
ARIvalue==0
gradeLevel=""
file.each_line { |line|
charCount+=line.size
for i in 1..line.length
if(line[i]=='.')
sentenceCount+=1
end
if(line[i]==' ')
{
wordCount+=1
}
end
end
}
ARIvalue==4.71*(charCount/wordCount)+0.5*(wordCount/sentenceCount)-21.43
case ARIvalue
when 1
gradeLevel="5-6 Kindergarten"
when 2
gradeLevel="6-7 First/Second Grade"
when 3
gradeLevel="7-9 Third Grade"
when 4
gradeLevel="9-10 Fourth Grade"
when 5
gradeLevel="10-11Fifth Grade"
when 6
gradeLevel="11-12 Sixth Grade"
when 7
gradeLevel="12-13 Seventh Grade"
when 8
gradeLevel= "13-14 Eighth Grade"
when 9
gradeLevel= "14-15 Ninth Grade"
when 10
gradeLevel= "15-16 Tenth Grade"
when 11
gradeLevel= "16-17 Eleventh Grade"
when 12
gradeLevel= "17-18 Twelth Grade"
when 13
gradeLevel= "18-24 College student"
when 14
gradeLevel="24+ Professor"
end
puts "Total # of Charecter: #{charCount}"
puts "Total # of words: #{wordCount}"
puts "Total # of sentences: #{sentenceCount}"
puts "Total # of Automated Readability Index: #{ARIvalue}"
puts "Grade Level: '#{gradeLevel}''"
else
puts "Unable to open file!"
end
end
and here are the errors
main.rb:37: syntax error, unexpected '\n', expecting =>
... wordCount+=1
... ^
main.rb:128: syntax error, unexpected keyword_end, expecting '}'
Remove curly braces around wordCount+=1
Related
I want to do something like that.
puts "Please write your age: "
age = gets.chomp
if #{age}<18
puts "you are illegal"
else #{age}>18
puts "You are legal"
end
the output i get is:
"Please write your age"
15.
you are illegal
you are legal"
and this
"Please write your age
20
you are illegal
you are legal"
Why?
And what is the solution please?
What I expect is this
If I write 19 or older, it will say "you are legal"
And if I write 17
or any number below
It will tell me "You are illegal"
Welcome to StackOverflow.
#{} is used for string interpolation, you don't need it there, and else statements don't work like this (elsif does). You also need to convert the string to an integer. You could write it like this:
puts "Please write your age: "
age = gets.chomp.to_i
if age > 18 # Since you want 19 or older. You could use age > 17 or age >= 18 if you actually meant 18 or older.
puts "You are of legal age"
else
puts "You are not of legal age"
end
See
The problem is that your code is equivalent to:
puts "Please write your age: "
age = gets.chomp
if
puts "you are illegal"
else
puts "You are legal"
end
Because # starts a comment, that makes the interpreter ignore everything after it on that line.
You can use any of the suggestions in the other answers to fix the code.
age = gets.chomp.to_i
if age<18
... to get integer-to-integer comparison.
You should first convert the input type to Integer and then make your logic. Note that is also important to check if the string input is numeric (since to_i returns 0 on cases like 'a'.to_i). You can do that like so:
puts 'Please write your age: '
# strip removes leading and trailing whitespaces / newlines / tabs
age = gets.strip
unless age.to_i.to_s == age
puts 'Age must be a number'
exit
end
age = age.to_i
if age < 18
puts 'you are illegal'
else
puts 'You are legal'
end
This isn't going to be easy to explain and I haven't found any answers for it.
I want to be able to read .txt file in Ruby and somehow be able to print the line number.
Example:
#file.txt:
#Hello
#My name is John Smith
#How are you?
File.open("file.txt").each do |line|
puts line
puts line.linenumber
end
#First Iteration outputs
#=> Hello
#=> 1
#Second Iteration outputs
#=> My name is John Smith
#=> 2
#Third Iteration outputs
#=> How are you?
#=> 3
I hope this makes sense and I hope it's easily possible.
Thanks in advance,
Reece
Ruby, like Perl, has the special variable $. which contains the line number of a file.
File.open("file.txt").each do |line|
puts line, $.
end
Prints:
#Hello
1
#My name is John Smith
2
#How are you?
3
Strip the \n from line if you want the number on the same line:
File.open("file.txt").each do |line|
puts "#{line.rstrip} #{$.}"
end
#Hello 1
#My name is John Smith 2
#How are you? 3
As stated in comments, rather than use File.open you can use File.foreach with the benefit of autoclose at the end of the block:
File.foreach('file.txt') do |line|
puts line, $.
end
# same output...
You can use Enumerable#each_with_index:
Calls block with two arguments, the item and its index, for each item
in enum. Given arguments are passed through to each().
File.open(filename).each_with_index do |line, index|
p "#{index} #{line}"
end
I am expecting to print each number, not just one big value. This is my code:
i = 20
loop {
i -= 1
print i
break if i <= 0
}
# >> 191817161514131211109876543210
I have tried:
printing just i
switching braces with do and end
changing the value of i
puts adds a new line after each argument passed to it (unless there's already a newline ending the arg).
print does not add a new line.
Therefore, you need to switch to puts:
i = 20
loop {
i -= 1
puts i
break if i <= 0
}
This is roughly equivalent to using the following code:
i = 20
loop {
i -= 1
print "i\n"
break if i <= 0
}
\n is the newline character:
\n Insert a newline in the text at this point.
Here's an example of passing multiple args to a puts statement, to really simplify your code:
puts *(0..20)
Or, in the reverse order you're using:
puts *19.downto(0)
This uses the splat operator to send the range as arguments to puts, giving you the output you're after,
Hope this helps - let me know if you've any questions.
You are using print which will print lines to the console without the \n at the end.
change print to puts.
as you already got your solution, you need to replace print with puts. I am showing you an alternate solution to get the same output:
puts (0..20).to_a.reverse
OR
(0..20).to_a.reverse.each{|i| puts i}
Output:
20
19
18
17
16
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1
0
I thought I understood the rules on indenting but almost every question I ask on S/O ends up with a comment "indent your code correctly". Just want to clear this up once and for all. This is a snippet from a project I am working on. What's indented correctly and what isn't?
def display
print " 1 2 3 4 5 6 7"
#board.each do |i|
print "\n"
#draw = "+-----+-----+-----+-----+-----+-----+-----+"
puts #draw
i.each do |n|
print "| #{n} "
end
print "|"
end
print "\n"
print #draw
end
Here is the good link to follow all the rules of the indentation
https://github.com/github/rubocop-github/blob/master/STYLEGUIDE.md
If I have a string within c1, I can print it in a line by doing:
c1.each_line do |line|
puts line
end
I want to give the number of each line with each line like this:
c1.each_with_index do |line, index|
puts "#{index} #{line}"
end
But that doesn't work on a string.
I tried using $.. When I do that in the above iterator like so:
puts #{$.} #{line}
it prints the line number for the last line on each line.
I also tried using lineno, but that seems to work only when I load a file, and not when I use a string.
How do I print or access the line number for each line on a string?
Slightly modifying your code, try this:
c1.each_line.with_index do |line, index|
puts "line: #{index+1}: #{line}"
end
This uses with with_index method in Enumerable.
Slightly modifying #sagarpandya82's code:
c1.each_line.with_index(1) do |line, index|
puts "line: #{index}: #{line}"
end
c1 = "Hey diddle diddle,\nthe cat and the fiddle,\nthe cow jumped\nover the moon.\n"
n = 1.step
#=> #<Enumerator: 1:step>
c1.each_line { |line| puts "line: #{n.next}: #{line}" }
# line: 1: Hey diddle diddle,
# line: 2: the cat and the fiddle,
# line: 3: the cow jumped
# line: 4: over the moon.