What is wrong with this Ruby code 2? -


what wrong code?

class person   def initialize(name)     @name = name   end   def greet(other_name)     "hi #{other_name}, name #{name}"   end end 

write code

class person   def initialize(name)     @name = name   end    def greet(other_name)    "hi #{other_name}, name #{@name}" # <~~ missed `@` before name.   end end 

if write name(instead of @name), ruby try local var named name, didn't define any. try check if method have defined name or not, not present.

undefined local variable or method `name'  

here example after fix :

#!/usr/bin/env ruby  class person   def initialize(name)     @name = name   end    def greet(other_name)    "hi #{other_name}, name #{@name}"    end end  person.new("raju").greet('welcome!') # => "hi welcome!, name raju" 

Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -