3. Create your own (a greeting function)


I’m sorry but I need a help.
I don’t understand why my programm doesn’t work ?I would like to create a greeting function.

def greeting puts “Hello”
Hello.each {|x| puts “#{x}”}

Can you fix it please ?



You say not working, but what do you mean by that? What is different from what you want?
Also please take care to mark any code as preformatted text so that the forum does not reformat it and therefore cause something other than your code to display in your post


Hi Ionathan!
Here the message error that I receive after to use the button “run”.

(ruby):1: syntax error, unexpected tSTRING_BEG, expecting ‘;’ or '\n’
_def greeting puts “Hello” _
_ ^_
(ruby):3: syntax error, unexpected keyword_end, expecting end-of-input

Please, could you help me ?

Have a nice day.



Do you mean this?

a.rb:9: syntax error, unexpected tSTRING_BEG, expecting ';' or '\n'
def greeting puts "Hello"
a.rb:11: syntax error, unexpected keyword_end, expecting end-of-input

You have to show it exactly as you see it. Otherwise it’s something else.

The first error message there says that it did not expect the start of a string, and it points at the start of a string.
The method name and its parameters are separate from the body of the method, yet you’ve put it all in the same place so what you wrote doesn’t match what ruby says is how a method is written.
Note also that your error message suggests either a semi-colon or a newline at that location (though you might want to put it somewhere else, it’s up to you to put things where they belong in order to mean what you mean)

You might also want to look at an example of a valid method to get a better idea of what it’s supposed to look like. Google is good for finding such things. Another thing you can do is to start out with a smaller amount of code, like a method that takes 0 arguments and does nothing, and then you can add more parts one at a time and run it each time to see if it’s still ok.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.