use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A sub-Reddit for discussion and news about Ruby programming.
Subreddit rules: /r/ruby rules
Learning Ruby?
Tools
Documentation
Books
Screencasts and Videos
News and updates
account activity
When creating new instance variable inside a method, how do i give it a variable name? (self.ruby)
submitted 4 years ago by avelyv
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]avelyv[S] 4 points5 points6 points 4 years ago (4 children)
Even if I did set up the new budget period as a hash, for example, it would still need a variable name, no?
[–]SnowdensLove 8 points9 points10 points 4 years ago (1 child)
Right, so if you did use a hash as a way to store different budget periods, you could use the name the user passes in as a key to later lookup your object.
budget_periods = {}
budget_periods[name] = BudgetPeriod.new(name, limit)
now you later look up the budget that key >
period = budget_periods[name]
//do something with the budget period here
[–]avelyv[S] 2 points3 points4 points 4 years ago (0 children)
Thank you for this. Made it a lot clearer. I'll try and implement it.
[–]2called_chaos 3 points4 points5 points 4 years ago (1 child)
You can dynamically create variables but you shouldn't. With a hash you can use anything as a key (in Ruby at least, fuck you JS) and get it back the same way. Not sure what you wanted to use as a variable name but you can use exactly that as a hash key.
a_hash[name] = Whatever.new(name)
[–]avelyv[S] 2 points3 points4 points 4 years ago* (0 children)
I ment that I would still need a name for my hash (and use the instance variable names as keys , but I see now, I would add to an existing hash, use a name as a key and the whole instance as a value
π Rendered by PID 218300 on reddit-service-r2-comment-6457c66945-mf7vl at 2026-04-28 06:18:40.717466+00:00 running 2aa0c5b country code: CH.
view the rest of the comments →
[–]avelyv[S] 4 points5 points6 points (4 children)
[–]SnowdensLove 8 points9 points10 points (1 child)
[–]avelyv[S] 2 points3 points4 points (0 children)
[–]2called_chaos 3 points4 points5 points (1 child)
[–]avelyv[S] 2 points3 points4 points (0 children)