Ruby: unexpected end-of-input, expecting keyword_end for if statement
Change the count++
to count += 1
Ruby does not support incremental operator.
If statement syntax error, unexpected keyword_end
There's no increment operator ++
in Ruby, instead use value += 1
.
Try with:
def solve(a0, a1, a2, b0, b1, b2)
aS = 0
bS = 0
alpha = [a0, a1, a2]
beta = [b0, b1, b2]
(0..2).to_a.each do |i|
aS += 1 if alpha[i] > beta[i]
bS += 1 if alpha[i] < beta[i]
end
[aS, bS]
end
p solve(1, 2, 3, 4, 5, 6)
# => [0, 3]
In Ruby you don't need to specify the return
it'll be the last value you put within a function (or scope), also you must want to transform to array your range (1..3), and as you're using every index of it to check in your alpha
and beta
arrays you'll get nil
when the index take the number 3, because there's no index 3 on those arrays, try instead with (0..2).to_a
To increment your initial variables use +=
instead ++
and to check the index of arrays you can do it with square brackets alpha[i]
(isn't needed to write with a dot, or as methods).
To return more than one value, try this time doing it as an array, just "plain" objects will throw syntax error
.
if ... then ... end
can be shortened as do_something if this_happens
Ruby Beginner- unexpected end-of-input, expecting keyword_end
You need one more end
:
puts "Welcome to Shapes"
print "How big do you want your shape? "
shape_size = gets
shape_size = shape_size.chomp
print "Outside letter: "
outside_letter = gets
outside_letter = outside_letter.chomp
print " Inside Letter: "
inside_letter = gets
inside_letter = inside_letter.chomp
puts "About to draw a shape #{shape_size} big"
puts "using #{outside_letter} for the edge"
puts "and #{inside_letter} for the inside"
width = shape_size
height=shape_size
1.upto(height) do |row|
if row==1
puts outside_letter * width
elsif row==height
puts outside_letter * width
else
middle= inside_letter * (width-2)
puts
"#{outside_letter}#{middle}#{outside_letter}"
end
end # <--- here
Since you are learning here, I felt compelled to add more detail:
When you have a block, such as the 1.upto(height) do |row|
in your code, that will always require an end
, as it is a block (think of it like a unit of code). Within that block, you are executing the code for each item within the enumerable (ex. array). In this case, your enumerable is an array of each whole number between 1 and the value of height
:
2.3.0 :005 > 1.upto(4) do |number|
2.3.0 :006 > puts "The number is: #{number}"
2.3.0 :007?> end
The number is: 1
The number is: 2
The number is: 3
The number is: 4
Ruby syntax error: unexpected keyword_end, expecting end-of-input
I think you are using case when what you really want is just plain ole if/else
You should only use case if you have one thing that you are testing which of several possibilities it is eg:
case my_string
when /blue/
do blue stuff
when /yellow/
do yellow stuff
when /red/
do red stuff
else
do something else
end
You should not use it to test multiple things eg what you are doing here:
case # nothing here to compare against the `when` cases
when type.match(/snapshot$/i) # here you are comparing type
when version.match(/latest$/i) # and now version
when type.match(/release$/i) # and now type again
also... you've got weird nesting that will not work. You code technically does this:
case # open the case statement
when type.match(/snapshot$/i) # first match
when version.match(/latest$/i) # second match
else # this counts as the else clause of the first case
end # this has now closed the case statement completely
# and now there's another when... without a case... which is why yuour code is borking
when type.match(/release$/i) # and now type again
What I think you're really trying to do is nest a second case statement inside the first one (if I'm wrong, correct me on this)>
If so... you really, really, really don't need case statements. Just use if/else like this
if type.match(/snapshot$/i) # first match
if version.match(/latest$/i) # second match
else # this counts as the else clause of the second if-statement
end # this has now closed the second if-statement
elsif type.match(/release$/i) # and now we test a second option against type
end # and this closes the first if-statement
if you realy, really must use case statements, then you need to do a few things:
a) actually specify what you're testign in the case-statement - right after the word case
b) repeat the keyword case
every time you are opening a new, nested set of them... eg:
case type
when /snapshot$/i # first case - first option (testing type)
case version
when /latest$/i # second case - first option (testing version)
else # else clause of second case
end # end of second case
when /release$/i # first case - second option (testing type)
end # end of first case
Note that the case-statement is absolutely overkill when there is only one thing to test against (eg the second case statement).
Ruby returns unexpected end-of-input, expecting keyword_end error
I have made a couple of changes to the code. seems like some compilers cant reach the German characters. I tried running using an online compiler and it was showing errors in the statements with these characters "..." and others too.
class String
def green; "\e[32m#{self}\e[0m" end
end
def load
require 'progress_bar'
bar = ProgressBar.new
100.times do
sleep 0.1
bar.increment!
end
load
def start
system("msfdb stop")
system("clear")
puts "STARTE SCRIPT[|]".green
sleep 1
system("clear")
puts "STARTE SCRIPT[/]".green
sleep 1
system("clear")
puts "STARTE SCRIPT[-]".green
sleep 1
system("clear")
puts "STARTE SCRIPT[\]".green
sleep 1
system("clear")
puts "[+] SCRIPT GESTARTET".green
sleep 2
system("clear")
puts "#####################################################################################################################################################################################".green
sleep 1
puts "#LINZENZ VON Qerim Iseni09 #".green
sleep 1
puts "#Bitte Verkauf es nicht #".green
sleep 1
puts "#Bitte modifiziere es nicht #".green
sleep 1
puts "#andere es nicht #".green
sleep 1
puts "#usw. #".green
sleep 1
puts "#Danke dass du es installiert hast. #".green
sleep 1
puts "# ########### ########## ####### ####### # # #".green
sleep 1
puts "# # # # # # # # # # # #".green
sleep 1
puts "# # # # # # # # # # # #".green
sleep 1
puts "# # # # # # # # # # # #".green
sleep 1
puts "# # # # ########## ####### # # # # # #".green
sleep 1
puts "# # # # # ## # # # # # #".green
sleep 1
puts "# # ## # # # # # # # # #".green
sleep 1
puts "# ############ # # # # # # # # #".green
sleep 1
puts "# # ########## # # ####### # ## # #".green
sleep 1
puts "#####################################################################################################################################################################################".green
sleep 5
system("clear")
system("msfdb start")
sleep 5
end
start
loop do
load
system("clear")
def danke_function
sleep 2
puts "DANKE FUER BENUTZEN!".green
puts "Programmiert von Qerim Iseni09".green
sleep 5
system("clear")
end
def easter_function
loop do
puts "DU HAST DEN EASTEREGG GEFUNDEN!".green
end
end
def addition_function
puts "Welche Nummern willst du ausrechnen?".green
n1 = gets.to_i
n2 = gets.to_i
puts "Rechne aus".green
sleep 2
puts "Resultat ist #{n1 + n2}".green
danke_function
end
def subtraction_function
puts "Welche Nummern willst du ausrechnen?".green
n1 = gets.to_i
n2 = gets.to_i
puts "Rechne aus".green
sleep 2
puts "Resultat ist #{n1 - n2}".green
danke_function
end
def multiplication_function
puts "Welche Nummern willst du ausrechnen?".green
n1 = gets.to_i
n2 = gets.to_i
puts "Rechne aus".green
sleep 2
puts "Resultat ist #{n1 * n2}".green
danke_function
end
def division_function
puts "Welche Nummern willst du ausrechnen?".green
n1 = gets.to_i
n2 = gets.to_i
puts "Rechne aus".green
sleep 2
puts "Resultat ist #{n1 / n2}".green
danke_function
end
def index
puts "Willst du */+- Rechnen?".green
response = gets.chomp
if response == "+" then
addition_function
end
if response == "-" then
subtraction_function
end
if response == "*" then
multiplication_function
end
if response == "/" then
division_function
end
if response == "S" then
easter_function
end
if response == "exit" then
danke_function
exit
end
if response == "qerim" then
puts "Qerim Wie Gehts?"
re = gets.chomp
puts "Also #{re}"
index
end
if response == "exit!" then
system("clear")
exit
end
if response == "execute" then
re2 = gets.chomp
system("#{re2}")
end
end
index
end
end
unexpected keyword_end, expecting end-of-input
The lines w/ do
in them should not have the .
@current_department.employees.each.do |c|
should be
@current_department.employees.each do |c|
do
is the keyword for beginning a block, not a method on each
Related Topics
Attr_Accessor, Not Able to Access Property
How to Add "Access-Control-Allow-Origin" Headers to API Response in Ruby
Ruby, No Implicit Conversion of Symbol into Integer
Rails 4 - Pundit - Create Policy
Heroku: Push Rejected, Failed to Compile Ruby App
How to Get Data from Database in Ruby
Resize Existing Images to New Style in Paperclip & Rmagick
Get File Name and Extension in Ruby
Ruby on Rails: Adding Columns to Existing Database
Ruby, MAC, Geektool Question, File Access Rights
Ruby on Rails Group_By (How to Group Events by Month)
Running Rake Task from Within War File
Show All the Table Entries That Belong to a Specific Entry in Rails
I Want to Match All Punctuation in My Regexp Except Apostrophes. How to Do That in Ruby
Why Are Database Entries Being Automatically Created When I Visit the "New" Page
Are Spies an Appropriate Approach to See If Resque Methods Are Being Fired
What Is a Very Simple Authentication Scheme for Sinatra/Rack