Home>
I want to make a program that changes greetings depending on the time, but I can't.
Currently it is up to the bottom.
def aisatsu (name)
time = Time.now # Get the present tense
case time.hour
when 5..10 # From 5 to 10
return "Good morning, # {name}!"
when 11..17 # From 11:00 to 17:00
return "# {name} 's, Hello!"
else # other than that
return "# {name}, good evening!"
end
end
result = aisatsu (ARGV [0])
printf ("% s% d time. \ N", result, time.hour)
The error is as below.
in `<main>': undefined local variable or method` time' for main: Object (NameError)
How can I fix it? Why not again?
-
Answer # 1
-
Answer # 2
Variables defined in a function (local variables) are valid only in that function.
So, at% d. I think it should be included in aisatu
-
Answer # 3
time = Time.now
Created intime
The variable isaisatsu
A local variable that is valid only within the method. If you try to refer to it outside the method, you will get the error "No such variable".I think it should be returned as the return value.
def aisatsu (name) time = Time.now # Get the present tense case time.hour when 5..10 # From 5 to 10 return "# {name}, good morning!", Time.hour when 11..17 # From 11:00 to 17:00 return "# {name} 's, Hello!", time.hour else # other than that return "# {name}, good evening!", Time.hour end end result, hour = aisatsu (ARGV [0]) printf (at "% s% d. \ n", result, hour)
Related articles
- ruby on rails 6 - error resolution for no route matches [delete]
- ruby on rails - i want to resolve rails name error
- ruby on rails - error with automatic deployment by capistrano
- ruby on rails 6 - [error] nomethoderror in communitiescontroller # create undefined method `published?'for # <community: 0x0
- ruby on rails - i want to download a file with rails, s3 and carrierwave, but i get an error
- ruby on rails - rails routing error
- ruby on rails 5 - i want to get an error message when i enter the user id in full-width in url
- ruby on rails - i get a template is missing error in render
- ruby on rails - unknown action error when trying to post a comment
- ruby on rails - test code causes error when product information input is successful
- ruby on rails 6 - [error] nomethoderror in followscontroller # destroy undefined method `destroy'for nil: nilclass
- ruby on rails - syntax error cannot be resolved
- error in building environment of ruby on rails
- ruby on rails - i don't understand the content of the error
- ruby on rails - i get an error with rails db: create
- ruby on rails 6 - [error] uninitialized constant user (call'userconnection' to establish a connection) :: image
- ruby on rails 6 - [rails]'unfollow' function does not work * no error message
- ruby on rails 6 - [error] nomethoderror in followscontroller # create undefined method `follows' for nil: nilclass
- ruby on rails 6 - i want to solve the error that occurred while implementing the product listing function
Trends
- python - you may need to restart the kernel to use updated packages error
- dart - flutter: the instance member'stars' can't be accessed in an initializer error
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- javascript - how to check if an element exists in puppeteer
- sh - 'apt-get' is not recognized as an internal or external command, operable program or batch file
- i want to check the type of a shell script variable
- i want to call a child component method from a parent in vuejs
Top-level method (function-like one)
aisatsu
Variables used in the definition oftime
Because the scope extends to the end of the definition.That is, run outside the definition
printf
From thattime
You cannot use the value of. Since it is treated as a separate variable, an error saying that it is undefined will occur.As another variable
time
ToTime.now
You can set the value of, but since it is used only in one place, it is directlyhour
Stick together,But I should write it.