Codecademy beginner ruby (methods)

I’m a beginner and have started Ruby on codecademy.com.

I was told to do something on codecademy which is giving me problems:

Define two methods in the editor:

A greeter method that takes a single string parameter, name, and returns
a
string greeting that person. (Make sure to use return and don’t use
print
or puts.)
A by_three? method that takes a single integer parameter, number, and
returnstrue if that number is evenly divisible by three and false if
not.

This is what I did:
def greeter(name)
return “Hey” + name
end

def by_three?(number)
if number % 3 == 0
return “true!”
else
return false
end

When I run it, it tells me there is a problem with the greeter method.
I’ve
tried multiple things, but nothing seems to work. I should say that the
codecademy.com console expects you to code it exactly how they want it,
else, it will return an error. Some other things I tried…

def greeter(name)
return “Hey” + name
end

name(“Phoebe”)

Thanks!

Hello,

On Sep 13, 2014, at 9:48, Cyril G. [email protected]
wrote:

I’m a beginner and have started Ruby on codecademy.com.

I was told to do something on codecademy which is giving me problems:

Define two methods in the editor:

A greeter method that takes a single string parameter, name, and returns a
string greeting that person. (Make sure to use return and don’t use print or
puts.)
A by_three? method that takes a single integer parameter, number, and
returnstrue if that number is evenly divisible by three and false if not.

Nice. Your code has a syntax error. You need to add an end to line 10,
in order to close the scope. Thats a very common mistake when writing
code, usually editors will complain a priori, or will let you know
that something is wrong using syntax highlighting. In Code Academy you
are writing code in the browser so, I dont remember if the syntax
highlighting works as expected.

    return false

end

When I run it, it tells me there is a problem with the greeter method. I’ve
tried multiple things, but nothing seems to work. I should say that the
codecademy.com console expects you to code it exactly how they want it, else, it
will return an error. Some other things I tried

Here is the error when I run the code above: test.rb:11: syntax error,
unexpected end-of-input, expecting keyword_end

The error explicitly states that you missed an end keyword. The line
line 11 is the line where the code gets executed.

Here is the code as it should be:

Greeter program

def greeter(name)
return "Hey " + name
end

def by_three?(number)
if number % 3 == 0
return “true!”
else
return false
end
end

def greeter(name)
return “Hey” + name
end

name("Phoebe)

You didnt define a method called name. Your method is called greeter!
Goes like greeter(Phoebe)

In order to run this code for CodeAcademy you might have to run
name(Phoebe), but in order to see the output in console/terminal you
should add puts, lil puts greeter(Phoebe).

Thanks!

Have a nice day.

Panagiotis (atmosx) Atmatzidis

email: [email protected]
URL: http://www.convalesco.org
GnuPG ID: 0x1A7BFEC5
gpg --keyserver pgp.mit.edu --recv-keys 1A7BFEC5

“As you set out for Ithaca, hope the voyage is a long one, full of
adventure, full of discovery […]” - C. P. Cavafy

for the greet method:

try using string interpolation, it’s very useful

def greeter(name)
return “Hello #{name}”
end

The interpolation allows you to call on a variable, you can also call
methods within the interpolation

For example, if you want to capitalize the name, you would change it to
#{name.capitalize}

When you are calling the greet method, a useful method to call on it
(while
testing) is the inspect method which can be done by either:
p greeter(name) or greeter(name).inspect

for the number problem:

you are requesting a true or false state, evaluating i%3==0

i%3 == 0 will already produce a true or false statement

Therefore, all you have to write is

def by_three?(num)
i % 3 == 0
end

This will give you either true or false
You do not have to do an if-else-if and “True!” or “False!”

When looking for a boolean value, all you need to do is write a
conditional
equation

Good luck! You’re doing good!

-Josh

On Fri, Sep 12, 2014 at 11:48 PM, Cyril G. <

Thank you guys for the help and the encouragement ! It’s going to be
a steady and long process, one that I’m looking forward to. I’m going to
have a lot of questions, some that may seem obvious, and so I hope you
guys
remain patient with me

My goal is to do at least 1 hour of coding everyday for the next 3
months.

Sorry for hijacking the thread.

However, I can’t figure out what is wrong with my greeters method, which
is a direct copy of what was posted in the earlier message

def greeter(name)
return “Hello #{name}”
end

"

When I tried to run the method by

greeter(phoebe)

I received this error

undefined local variable or method `phoebe’ for main:Object (NameError)

Can anyone please explain why this happens?

Thank you in advance.

phoebe is a local variable. “phoebe” is a string.