I am unable to execute this program - ruby

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.

Related

NilCheck fix on safe navigation operator (&.)

This simple method on a class just run the status method using the safe navigation operator.
def current_status
account&.status
end
But reek report this warning:
MyClass#current_status performs a nil-check [https://github.com/troessner/reek/blob/master/docs/Nil-Check.md]
How can I properly write methods like this to avoid Nil Check?
I've also verified this post from thoughtbot but it seem like "too much" for just a safe navigation operator.
Ruby 2.3.1
The advice from "Example 4" in the linked post is verbose but pretty good :
class MyClass
def initialize(with_account = nil)
#account = Account.new if with_account
end
def current_status
account.status
end
def account
#account || NilAccount.new
end
end
class Account
def status
"Up!"
end
end
class NilAccount
def status
"Down!"
end
end
puts MyClass.new(:with_account).current_status
#=> "Up!"
puts MyClass.new.current_status
#=> "Down!"
If it's "too much" for you, account&.status might be just fine.
Whatever you do : you'll need to test your code as much as possible!
well, tell-dont-ask looks pretty good, but Example 4 looks like an overkill to resolve this specific case.
#andredurao I think, we can use this workaround to pass checks, for some reason reek is fine with it:
def current_status
return unless account
account.status
end

Ruby Koans class or module required

I am getting the following error in Ruby Koans:
AboutHashes#test_accessing_hashes_with_fetch has damaged your karma.
The Master says:
You have not yet reached enlightenment.
I sense frustration. Do not be afraid to ask for help.
The answers you seek...
class or module required
Please meditate on the following code:
/home/s/Downloads/github/rubykoans/about_hashes.rb:26:in `test_accessing_hashes_with_fetch'
The line in question is part of the following method:
def test_accessing_hashes_with_fetch
hash = { :one => "uno" }
assert_equal "uno", hash.fetch(:one)
assert_raise(nil) do
hash.fetch(:doesnt_exist)
end
As you can see, it is asking for a class or module, so I am confused as this is not an error I've encountered before in the Koans.
The problem is on these lines:
assert_raise(nil) do
hash.fetch(:doesnt_exist)
end
The assert_raise test macro expects as its argument a class of Exception. You provided nil.
To skip to the answer, calling fetch on a hash with a key that doesn't exist in the hash will raise a KeyError Exception. So the test should have this:
assert_raise(KeyError) do
hash.fetch(:doesnt_exist)
end

Building proper class and passing a class method

class Health
def initialize()
#hydration=hydration
end
def hydration
puts"Amount of Water drunk(in oz):"
x=gets.chomp
if #{x}>=20
puts"Good job! keep on it"
elsif #{x} >=(10...20)
puts"Could do better"
else
puts"Not healthy. Go get hydration"
end
end
drinks=Health.new()
puts drinks.hydration
end
I am new to ruby and what i'm trying to achieve is to be prompted. i'm getting the "syntax error, unexpected end-of-input, expecting keyword_end" on the puts drinks.hydration
I dont think you should get any syntax error with your current code. In order to make it working you need to remove the comments in if statements. Also the initialize statement is redundant as you are making explicit call to the method hydration in puts drinks.hydration.
class Health
def hydration
puts "Amount of Water drunk(in oz):"
x=gets.chomp.to_i
if x>=20
puts "Good job! keep on it"
elsif (10...20).include? x
puts "Could do better"
else
puts "Not healthy. Go get hydration"
end
end
end
drinks=Health.new()
puts drinks.hydration

Is there any issues to have `end` as a method name?

I am working on a RoR project, and I want to know if I can use end as a method name. It seems to work fine, but I would like to know if this method will bring any issues in the future. I tried and it works:
class Dany
def end
puts 'Hola'
end
end
and this is the output:
Dany.new.end # => Hola
Ruby let's you do this, but you're going to run into all sorts of issues.
# end.rb
class Dany
def end
puts "Hola"
end
def other
end # should puts Hola
end
end
Instead, you will get
end.rb:10: syntax error, unexpected keyword_end, expecting end-of-input
Bottom line: don't do this. Don't use any keywords as a method name.
It is not a good idea to use a keyword as a method name, but as long as you disambiguate the token as a method call, you can use it. It is not practical though.
Dany.new.instance_eval{self.end} # => Hola
Dany.new.send(:end) # => Hola
Dany.new.method(:end).call # => Hola
Dany.new.instance_eval{end} # => syntax error, unexpected keyword_end
The usual disambiguation using () does not seem to work for this case, making it complicated.
Dany.new.instance_eval{end()} # => syntax error, unexpected keyword_end

Rubywarrior Level 4 (cleaning up my code help)

I'm learning programming through Ruby and I found the awesome Rubywarrior by Ryan Bates from Railscasts. Unfortunately I'm stuck in that my code is throwing up syntax error messages (unexpected $end).
I'm not asking for the answer, I would like to sort that out myself, but if someone could point out where my code is getting the error from that would be super. Thanks!
class Player
def initialize
#maxhealth = 20
#dying = 7
#previoushealth = #maxhealth
#health = warrior.health
#warrior = warrior
end
def play_turn(warrior)
# If there are no enemies, rest until health is 100%
turn_start_check(warrior)
actions(warrior)
turn_end_check(warrior)
end
def actions(warrior)
if #damaged_since_last_turn
warrior.shoot!
elsif
#health < #maxhealth
warrior.rest!
else
warrior.walk!
end
end
def hurt?(warrior)
warrior.health < 20
end
def healthy?(warrior)
warrior.health = 20
end
def alone?(warrior)
warrior.feel.empty?
end
def should_i_move?(warrior)
if healthy? and alone?
warrior.rest!
else
warrior.walk!
end
# Put code here for if health from previous turn is less than last term
# If true don't rest and keep moving forward
def turn_start_check(warrior)
#damaged_since_last_turn = #previoushealth > warrior.health
end
def turn_end_check(warrior)
#previoushealth = warrior.health
end
end
My guess:
def should_i_move?(warrior)
if healthy? and alone?
warrior.rest!
else
warrior.walk!
end # <<MISSING THIS ONE
end
That error message means that you are missing an end keyword somewhere. Check your code to see if all your statements are properly written.
With Ruby 1.9.3, if you turn on warnings, you get the following warnings:
-:46: warning: mismatched indentations at 'end' with 'if' at 42
-:57: warning: mismatched indentations at 'end' with 'def' at 41
and then the error
-:57: syntax error, unexpected $end, expecting keyword_end
line 46 corresponds to the first end after def should_i_move?(warrior)
This should work with earlier versions of Ruby 1.9 as well.

Resources