3 Replies - 744 Views - Last Post: 04 May 2013 - 12:46 AM Rate Topic: -----

#1 tessierny  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 12-February 13

Method syntax producing error

Posted 03 May 2013 - 07:42 PM

Greetings all,

I am learning Ruby using codecademy and the current exercise tells the user to :

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 returns true if that number is evenly divisible by three and false if not. Remember, it's a Ruby best practice to end method names that produce boolean values with a question mark.

Based on that I came up with some code but it doesn't work and I do not know how to fix it or what I am missing. Any push in the right direction is greatly appreciated! Here is my code :

def greeter (name)

name = gets.chomp
return "Hi there #{name} sucka!"
end

def by_three(number)

number = gets.chomp
if number % 3 == 0
return true
else return false
end




Is This A Good Question/Topic? 0
  • +

Replies To: Method syntax producing error

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Method syntax producing error

Posted 03 May 2013 - 07:54 PM

if blocks need to have an end as well:

def greeter (name)
  name = gets.chomp
  return "Hi there #{name} sucka!"
end

def by_three(number)

  number = gets.chomp
  if number % 3 == 0
    return true
  else 
    return false
  end
end



single line:

if condition then statement end

if condition then statement
else statement end

#etc...


Was This Post Helpful? 2
  • +
  • -

#3 tessierny  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 12-February 13

Re: Method syntax producing error

Posted 03 May 2013 - 08:19 PM

Ah, totally! The magic is in the details..Thank you for that one! I appreciate it

View PostKYA, on 03 May 2013 - 06:54 PM, said:

if blocks need to have an end as well:

def greeter (name)
  name = gets.chomp
  return "Hi there #{name} sucka!"
end

def by_three(number)

  number = gets.chomp
  if number % 3 == 0
    return true
  else 
    return false
  end
end



single line:

if condition then statement end

if condition then statement
else statement end

#etc...


Was This Post Helpful? 0
  • +
  • -

#4 Lemur  Icon User is offline

  • Pragmatism over Dogma
  • member icon


Reputation: 1359
  • View blog
  • Posts: 3,425
  • Joined: 28-November 09

Re: Method syntax producing error

Posted 04 May 2013 - 12:46 AM

View PostKYA, on 03 May 2013 - 09:54 PM, said:

if blocks need to have an end as well:

def greeter (name)
  name = gets.chomp
  return "Hi there #{name} sucka!"
end

def by_three(number)

  number = gets.chomp
  if number % 3 == 0
    return true
  else 
    return false
  end
end



single line:

if condition then statement end

if condition then statement
else statement end

#etc...



Ruby has implicit return methods, so it would work better in Ruby style as such:


def greeter(name)
  "Hi there #{name}"
end

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




Makes it a bit cleaner. I'd keep the user input outside of functions and keep functions as lean as possible.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1