简体   繁体   中英

Ruby getting information from a method

I am very new in Ruby and I have two methods here and I want to be able to get the email from one method in the other one. But not sure how to do that. Here we have a curly braces inside of brackets, does that means a hash inside an array? How do I deal with that? Thank you for all the help.

def myMethod
{
   name: "blabla"
   email: "That's what I want to get from the code below #{secondMethod}"
   description: "blablabla"
}
end

def secondMethod
  [{
     name: ....
     date: .....
     email: "I want this one not the next one"
     .
     .    
     .
     .
     .

},{
    name:....
    email: ....
     .
     .
}
]
end


secondMethod returns an array of hashes. And you only want the email of the first hash. This could be done like this:

def myMethod
  email = secondMethod.first[:email]      

  {
     name: "blabla"
     email: "That's what I want to get from the code below #{email}"
     description: "blablabla"
  }
end

Btw the usually Ruby naming convention uses underscore method names (not camelcase) which means your methods should be named my_method and second_method to follow those conventions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM