Ruby Gosu, my script gets and error with keyword end - ruby

The error is:
C:/Users/Admin/Desktop/MyFirstSelfMadeGame.rb:18: syntax error, unexpected keyword_end, expecting $end
Code:
#!/usr/bin/env ruby
require "gosu"
class GameWindow < Gosu::Window
def initialize(800, 600, false) #Window declaration
super
self.caption("Pokemon")
end
def update
end
def draw
end
def button_down(id)
close if id == Gosu::KbSpace
end
end
GameWindow.new.show
Thanks for answers, i get this problem alot.

No, this is not the whole error, the whole error looks something like:
t.rb:6: syntax error, unexpected tINTEGER, expecting ')'
def initialize(800, 600, false) #Window declaration
^
t.rb:21: syntax error, unexpected kEND, expecting $end
Please note that there are 2 error messages reported by the Ruby interpreter. You have noticed and posted only the 2nd one, but the 1st one is actually the relevant one. In general, if you receive error messages, it's a good rule of thumb to find and fix the 1st one first, because the subsequent ones may be caused by the first one.
You need to specify for function arguments. Incorrect:
def initialize(800, 600, false) #Window declaration
Correct:
def initialize()
super(800, 600, false)

As pts says, you cannot reference objects in a method definition arguments
The method definition arguments should only contain variables that you reference in the method.
If you want to use defaults but have the option to override the arguments when calling the 'new' method you could do...
def initialize(width=800, height=600, full_screen=false)
super(width, height, full_screen)
Also, you should note that self.caption is a getter method and doesn't take arguments
this is wrong
self.caption("Pokemon") # < wrong
this is right
self.caption = "Pokemon" # < right
Take a look at the tutorial game...
https://github.com/jlnr/gosu/wiki/Ruby-Tutorial

Related

Unexpected tIDENTIFIER in function declaration with optional argument

I am trying to declare a class with a few basic functions in it. The function that seems to be causing a problem has an optional argument that passes a symbol in.
class Bag < RandomizerCollection
def initialize()
end
def select(description:Hash, amt=:all)
end
def empty()
end
end
And the error I am getting is:
Traceback (most recent call last):
1: from test.rb:5:in `<main>'
test.rb:5:in `require_relative': /home/osboxes/Documents/Year4/Design/A1/Bag.rb:9: syntax error, unexpected tIDENTIFIER (SyntaxError)
...ef select(description:hash, amt = :all)
... ^~~
/home/osboxes/Documents/Year4/Design/A1/Bag.rb:9: syntax error, unexpected ')', expecting keyword_end
...t(description:hash, amt = :all)
I'm sure this must be something basic but I just can't figure it out. I am new to Ruby and I found similar questions but none helped me find the issue. Any help is appreciated!
You can't define optional arguments (arg=value) after the definition of the keyword arguments (arg: value).
You can correct it in two ways:
Move optional arg before the keywor arg:
def select(amt=:all, description:Hash)
end
Make the second argument a keyword arg:
def select(description:Hash, amt: :all)
end
Worth reading: https://medium.com/podiihq/ruby-parameters-c178fdcd1f4e

I am unable to execute this program

error:C:\Users\RR\Desktop\ruby_sandbox>ruby classes.rb classes.rb:44:
syntax error, unexpected end-of-input, expecting keyword_end
my code is:
class Animal
attr_accessor :name,:age,:sex,:location
def initialize(age=18,sex="not available",location="not specified")
puts "details of animal"
#age=age
#sex=sex
#location=location
end
def condition(age,name)
if animal.age>animal1.age
puts "#{animal.name } is older than #{animal1.name}"
else
puts "animals age are in increasing order"
end
end
Please take care about your indentation while writing ruby you will see where you missed the end keyword.
But in your case the problem is not only about the indentation before start to fix it. You can check some documentation about Class and Instance Methods in Ruby or this tutorial can help you in your case.
Good luck.

Rspec error in ruby code testing

Rspec code is
it "calls calculate_word_frequency when created" do
expect_any_instance_of(LineAnalyzer).to receive(:calculate_word_frequency)
LineAnalyzer.new("", 1)
end
Code of class is
def initialize(content,line_number)
#content = content
#line_number = line_number
end
def calculate_word_frequency
h = Hash.new(0)
abc = #content.split(' ')
abc.each { |word| h[word.downcase] += 1 }
sort = h.sort_by {|_key, value| value}.reverse
puts #highest_wf_count = sort.first[1]
a = h.select{|key, hash| hash == #highest_wf_count }
puts #highest_wf_words = a.keys
end
This test gives an error
LineAnalyzer calls calculate_word_frequency when created
Failure/Error: DEFAULT_FAILURE_NOTIFIER = lambda { |failure, _opts| raise failure }
Exactly one instance should have received the following message(s) but didn't: calculate_word_frequency
How I resolve this error.How I pass this test?
I believe you were asking "Why do I get this error message?" and not "Why does my spec not pass?"
The reason you're getting this particular error message is you used expect_any_instance_of in your spec, so RSpec raised the error within its own code rather than in yours essentially because it reached the end of execution without an exception, but without your spy being called either. The important part of the error message is this: Exactly one instance should have received the following message(s) but didn't: calculate_word_frequency. That's why your spec failed; it's just that apparently RSpec decided to give you a far less useful exception and backtrace.
I ran into the same problem with one of my specs today, but it was nothing more serious than a failed expectation. Hopefully this helps clear it up for you.
The entire point of this test is to insure that the constructor invokes the method. It's written very clearly, in a very straight forward way.
If you want the test to pass, modify the constructor so it invokes the method.

ruby *args syntax error

I found this weirdness that I would like to understand. If I define these two methods in pry...
def test(*args)
puts args
end
def test=(*args)
puts args
end
they both work.But if I put the above code in a module and include that module in another class (say, class Job), the following
j=Job.last
j.test=(1,2,3)
throws the following error...
SyntaxError: (irb):3: syntax error, unexpected ',', expecting ')'
j.test=(1,2,3)
^
The following work as expected...
j.test=[1,2,3]
j.test=(1)
So, it looks like inside the module, a method defined with an '=' always expects one arg. That doesn't make sense to me.
What am I missing
Parsing of the Ruby interpreter. Try
j.send :test=, 1, 2, 3
use directly
j.test = 1,2,3
or
j.test= ([1,2,3])
or `
j.send('test=',[1,2,3])

Does should_receive do something I don't expect?

Consider the following two trivial models:
class Iq
def score
#Some Irrelevant Code
end
end
class Person
def iq_score
Iq.new(self).score #error here
end
end
And the following Rspec test:
describe "#iq_score" do
let(:person) { Person.new }
it "creates an instance of Iq with the person" do
Iq.should_receive(:new).with(person)
Iq.any_instance.stub(:score).and_return(100.0)
person.iq_score
end
end
When I run this test (or, rather, an analogous one), it appears the stub has not worked:
Failure/Error: person.iq_score
NoMethodError:
undefined method `iq_score' for nil:NilClass
The failure, as you might guess, is on the line marked "error here" above. When the should_receive line is commented out, this error disappears. What's going on?
Since RSpec has extended stubber functionality, now following way is correct:
Iq.should_receive(:new).with(person).and_call_original
It will (1) check expectation (2) return control to original function, not just return nil.
You're stubbing away the initializer:
Iq.should_receive(:new).with(person)
returns nil, so Iq.new is nil. To fix, just do this:
Iq.should_receive(:new).with(person).and_return(mock('iq', :iq_score => 34))
person.iq_score.should == 34 // assert it is really the mock you get

Resources