Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Tried to include a module in another one, but something goes wrong
ruby pipboy.rb
pipboy.rb:3: syntax error, unexpected '<', expecting ';' or '\n'
def Pipboy < Person
^
pipboy.rb:22: syntax error, unexpected keyword_end, expecting end-of-input
def is a keyword to define methods. You probably want to derive a class/module. This is to be done with:
class Pipboy < Person
to include (as stated in OP) one module into other, one should use include keyword:
class Pipboy
include Person
...
The def keyword is used to create function definitions. What you are thinking of is the class keyword.
Make sure you have a Pipboy class, and then do class Pipboy < Person
Here is more information on inheritance
http://rubylearning.com/satishtalim/ruby_inheritance.html
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
Is a simple question about how to print an interpolation on a string in ruby
My method is this:
def hello(parameter)
puts "hello + #{parameter}"
end
puts hello(name)
but irb, show me these message:
NameError: undefined local variable or method `name' for main:Object
It is about my commands to irb? or what i'm doing wrong in my code, 'cause i know that i 've a mistake but can't find where.
Thank you, it's my first post ejje.
The issue lies in your call to hello(name). name in this case is treated at the name of a variable, which you have not defined. If you meant for the console to print "hello + name", then you should call the function with the string "name", surrounded by quote marks: hello("name").
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
So I'm defining a anonymous class to test a method like so:
let!(:s_integer) do
class << self
def change(value)
self(value)
end
end
def change(value)
self.class.change(value)
end
end
The problem is that where Integer('9') works, self('9') doesn't, but I can't use to_i so how do I work around this problem?
Integer(arg, base=0) is a method in Kernel, not in Integer. So you can't use self to call it.
def change(value)
Integer(value)
end
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
What is the syntax error in this short ruby block?
def do_this (a,b,c)
puts 1.to_s, 2.to_s,3.to_s
if a == 4
do_this (1,2,3)
end
end
do_this (4,5,6)
I get errors on the fourth and seventh lines, where "do_this" is called.
The error is: 'unexpected ")", expecting "." or...' [...]
Remove spaces between method name and parentheses.
You need to avoid using spaces between the method call and the arguments in parenthesis:
def do_this (a,b,c)
puts 1.to_s, 2.to_s,3.to_s
if a == 4
do_this(1,2,3)
end
end
do_this(4,5,6)
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have this code:
Class Card
def initialize(suit, number)
#suit = suit
#number = number
end
end
I am getting the error message:
/go_fish/lib/Card.rb:8 syntax error, unexpected keyword_end, expecting end-of-input (SyntaxError)
Any suggestions or stuff I'm missing?
Your error is the first char in the first line. class should be lowercase...
class Card
def initialize(suit, number)
#suit = suit
#number = number
end
end
Furthermore you may want to follow common rules for coding styles in Ruby. To do so change the file name to card.rb (again lowercase), same with require. And it is common to indent with just two whitespace characters in Ruby files.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
? and ! are used in method names, but apparently cannot be used in variable names?
foo! = 2
=> SyntaxError: (irb):1: syntax error, unexpected '='
What is the reason?
? and ! are ruby operators, so they are not allowed in variable names. Otherwise, how will Ruby evaluate something like if (v!=2) (expression that checks whether a variable v is not equal to 2) or something like v?1:0 (expression that will return 1 if v is truthy and 0 if its falsy)
UPDATE
Another plausible reason is that Ruby treats names ending with ? and ! as methods