Practice Makes Perfect


#1

def greeter(name)
return ("Hello, " + name)
end

def by_three?(n)
if n % 3 == 0 then
puts true
else
puts false
end
end

greeter("abc")

by_three?(138)

by_three?() works fine, but the greeting is messing with me.

Thanks


#2

@myistek

Hi,

Shouldn't be parentheses around "Hello" + name


#3

Changed it and it still says my greeter is printing instead of returning


#4

instead of 'return' do print and it does return!


#5

The excercise says to no use print, or puts. It works for me, and will print the greeting if I delete the whole by three definition.

def greeter(name)
return "Hello," + name
end
greeter(" carlos")

def by_three?(number)
return number % 3 == 0
end
by_three?(6)


#6

Interpolate name #{ }


#7

def by_three(n)
if n % 3 == 0
return true
else
return false
end
end
by_three(9)

def greeter(name)
return "Hello," + name
end

greeter(" carlos")


#8

WHY CAN'T show two answer at the same time?


#9

Can anyone please tell me why the following code is not working?

def greeter(name)
return "Hello #{name}!"
end

greeter (Adam)

This prints nothing. Why does it not print Adam?

edit: I got the same problem in #17 Methods With Arguments:

I did put in there:

def welcome(name)
return "Hello, #{name}!"
end

welcome(adam)

After save&submit I can "Start Next Lesson" but there's an error at the right:

undefined local variable or method `adam' for ##

What do i do wrong?


#10

Couple of things.

  1. The undefined local variable is because the interpreter thinks adam is a variable. Since you're trying to pass a string directly into the method instead of a variable, you should quote it, like so:
    welcome("adam")

  2. Your method is returning a string, so when you call the method that string will be returned as expected. However, if you want it to print the result to the screen you need to ask for that explicitly, like so:
    puts welcome("adam")

Hope that helps


#11

It helped. Thank you!


#12

I had the same problem and there apparently is a mistake in the instructions as it worked if i printed the greeting instead of returning it:

def greeter(name)
print name.to_s #if i use return here it says i shouldn't print the statement to the console
end

def by_three?(number)
if number.is_a? Integer
return number % 3 == 0
else
puts "No integer"
end
end


#13

def greeter(name)
return "Thanks #{name}"
end

greeter("habib")

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

what's wrong in my code?


#14

...
...
...
def by_three?(n)
if
n % 3 == 0
return true
else
return false
end
end
by_three?(gets.chomp)

Why it always return "false"?