Maybe I've been staring at my Ruby book to the point where words don't mean anything anymore, but I thought I might as well ask.
What I'm looking to do is, rather than passing a bulk of variables through my function headers, or instead of using global variables, I'm looking to save my variables within a method, and call upon them at multiple times, throughout my functions. What I'm realistically having an issue with, is scope.
def DateGrab()
print "\nEnter the date you're looking for (Month/Day): "
longdate = gets.strip.split(/\/| /)
if longdate[0].length > 3
month = longdate[0].slice(0..2)
else
month = longdate[0]
end
day = longdate[1]
year = `date | awk '{print $6}'`.strip
grepdate = "#{day}/#{month}/#{year}"
date = Date.parse("#{day}-#{month}-#{year}").strftime('%m%d%Y').strip
end
I'm looking to pass "grepdate" and "date" through multiple functions, and I feel that using a method would be easier, but every time I try to call the variable, I get a "undefined local variable or method" error.
You want to look at instance variables. You can set them via @grepdate = "something"
These instance variables are accessible throughout your class and in all methods of that class.
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.