Show age of userHow to find where a method is defined at runtime?Rails :include vs. :joinsdelete_all vs destroy_all?Rails 3: How to use a ruby gem for date validation for non-model dataRuby Filtering issueRails 4 Authenticity TokenVariable Scope ConfusionUse of float and multiple 'elsif' statements in Leap Year exampleCreating a Users list that is personalized by subdomain RailsI trying to make a code that gives the user a personal number after they have made an user
Algorithms vs LP or MIP
Shouldn't the "credit score" prevent Americans from going deeper and deeper into personal debt?
Justifying the use of directed energy weapons
LeetCode: Pascal's Triangle C#
Singleton Design Pattern implementation in a not traditional way
Can't stopover at Sapporo when going from Asahikawa to Chitose airport?
Why isn't "I've" a proper response?
What is the hex versus octal timeline?
Is “I am getting married with my sister” ambiguous?
Efficiently pathfinding many flocking enemies around obstacles
Notepad++ - How to find multiple values on the same line in any permutation
Does a face-down creature with morph retain its damage when it is turned face up?
Why is Boris Johnson visiting only Paris & Berlin if every member of the EU needs to agree on a withdrawal deal?
If the first law of thermodynamics ensures conservation of energy, why does it allow systems to lose energy?
Defense against attacks using dictionaries
In an emergency, how do I find and share my position?
Mathematical uses of string theory
Does travel insurance for short flight delays exist?
Irish Snap: Variant Rules
I have a player who yells
Cross-referencing enumerate item
Attaching a piece of wood to a necklace without drilling
What is the difference between true neutral and unaligned?
What is this symbol: semicircles facing eachother
Show age of user
How to find where a method is defined at runtime?Rails :include vs. :joinsdelete_all vs destroy_all?Rails 3: How to use a ruby gem for date validation for non-model dataRuby Filtering issueRails 4 Authenticity TokenVariable Scope ConfusionUse of float and multiple 'elsif' statements in Leap Year exampleCreating a Users list that is personalized by subdomain RailsI trying to make a code that gives the user a personal number after they have made an user
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I need to ask the user for their birth year, and then display every year from the birth year to 2017 with the age for each year.
I started with this:
puts "when are you born (year) ?"
birth_year = gets.to_i
birth_year.upto(2017)birth_year
I miss the part to display the age. I tried some stuff like:
puts birth_year.upto(2017) - birth_year
but it did not work.
ruby
add a comment |
I need to ask the user for their birth year, and then display every year from the birth year to 2017 with the age for each year.
I started with this:
puts "when are you born (year) ?"
birth_year = gets.to_i
birth_year.upto(2017)birth_year
I miss the part to display the age. I tried some stuff like:
puts birth_year.upto(2017) - birth_year
but it did not work.
ruby
Your block variable name is semantically incorrect. Rename it and then you'll easily see the correct calculation.
– Sagar Pandya
Mar 27 at 17:01
"Did not work" is not a useful diagnostic. Error messages, especially the exact text of those, is a lot better.
– tadman
Mar 27 at 17:20
add a comment |
I need to ask the user for their birth year, and then display every year from the birth year to 2017 with the age for each year.
I started with this:
puts "when are you born (year) ?"
birth_year = gets.to_i
birth_year.upto(2017)birth_year
I miss the part to display the age. I tried some stuff like:
puts birth_year.upto(2017) - birth_year
but it did not work.
ruby
I need to ask the user for their birth year, and then display every year from the birth year to 2017 with the age for each year.
I started with this:
puts "when are you born (year) ?"
birth_year = gets.to_i
birth_year.upto(2017)birth_year
I miss the part to display the age. I tried some stuff like:
puts birth_year.upto(2017) - birth_year
but it did not work.
ruby
ruby
edited Mar 27 at 16:59
sawa
136k31 gold badges220 silver badges317 bronze badges
136k31 gold badges220 silver badges317 bronze badges
asked Mar 27 at 16:42
antoineantoine
52 bronze badges
52 bronze badges
Your block variable name is semantically incorrect. Rename it and then you'll easily see the correct calculation.
– Sagar Pandya
Mar 27 at 17:01
"Did not work" is not a useful diagnostic. Error messages, especially the exact text of those, is a lot better.
– tadman
Mar 27 at 17:20
add a comment |
Your block variable name is semantically incorrect. Rename it and then you'll easily see the correct calculation.
– Sagar Pandya
Mar 27 at 17:01
"Did not work" is not a useful diagnostic. Error messages, especially the exact text of those, is a lot better.
– tadman
Mar 27 at 17:20
Your block variable name is semantically incorrect. Rename it and then you'll easily see the correct calculation.
– Sagar Pandya
Mar 27 at 17:01
Your block variable name is semantically incorrect. Rename it and then you'll easily see the correct calculation.
– Sagar Pandya
Mar 27 at 17:01
"Did not work" is not a useful diagnostic. Error messages, especially the exact text of those, is a lot better.
– tadman
Mar 27 at 17:20
"Did not work" is not a useful diagnostic. Error messages, especially the exact text of those, is a lot better.
– tadman
Mar 27 at 17:20
add a comment |
1 Answer
1
active
oldest
votes
You can use a different form of block like this:
puts "when are you born (year) ?"
birth_year = gets.to_i
birth_year.upto(2017) do |iterating_year|
puts "Year: #iterating_year"
puts "Age: #iterating_year - birth_year "
end
Or if you want it all in the same line try this - but it's less readable:
birth_year.upto(2017) iterating_year
Naming block variables
- Recommendation: Do not use the same variable name in your block as you have outside the block - that's just confusing. In this particular case, with the code as you have written it, the
birth_year
you have defined outside the block will have fallen out of scope, and the block parameter will take on the iterating years value: 2001, 2002, 2003 etc.
String Interpolation
This is when join strings together. One method you could use is to write some ruby code inside a string - you need a hash tag and opening and closing curly brackets to make it work. See this link for more info or google "ruby string interpoloation".
1
Since the OP appears to be new to Ruby, you may wish to mention string interpolation.
– Sagar Pandya
Mar 27 at 17:08
2
Ruby will actually warn about "variable shadowing" which is using the same name twice in different, overlapping scopes, if the-w
flag is enabled. It's a good idea to avoid it not only because it's confusing, but because it can lead to unexpected results.
– tadman
Mar 27 at 17:21
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55382431%2fshow-age-of-user%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use a different form of block like this:
puts "when are you born (year) ?"
birth_year = gets.to_i
birth_year.upto(2017) do |iterating_year|
puts "Year: #iterating_year"
puts "Age: #iterating_year - birth_year "
end
Or if you want it all in the same line try this - but it's less readable:
birth_year.upto(2017) iterating_year
Naming block variables
- Recommendation: Do not use the same variable name in your block as you have outside the block - that's just confusing. In this particular case, with the code as you have written it, the
birth_year
you have defined outside the block will have fallen out of scope, and the block parameter will take on the iterating years value: 2001, 2002, 2003 etc.
String Interpolation
This is when join strings together. One method you could use is to write some ruby code inside a string - you need a hash tag and opening and closing curly brackets to make it work. See this link for more info or google "ruby string interpoloation".
1
Since the OP appears to be new to Ruby, you may wish to mention string interpolation.
– Sagar Pandya
Mar 27 at 17:08
2
Ruby will actually warn about "variable shadowing" which is using the same name twice in different, overlapping scopes, if the-w
flag is enabled. It's a good idea to avoid it not only because it's confusing, but because it can lead to unexpected results.
– tadman
Mar 27 at 17:21
add a comment |
You can use a different form of block like this:
puts "when are you born (year) ?"
birth_year = gets.to_i
birth_year.upto(2017) do |iterating_year|
puts "Year: #iterating_year"
puts "Age: #iterating_year - birth_year "
end
Or if you want it all in the same line try this - but it's less readable:
birth_year.upto(2017) iterating_year
Naming block variables
- Recommendation: Do not use the same variable name in your block as you have outside the block - that's just confusing. In this particular case, with the code as you have written it, the
birth_year
you have defined outside the block will have fallen out of scope, and the block parameter will take on the iterating years value: 2001, 2002, 2003 etc.
String Interpolation
This is when join strings together. One method you could use is to write some ruby code inside a string - you need a hash tag and opening and closing curly brackets to make it work. See this link for more info or google "ruby string interpoloation".
1
Since the OP appears to be new to Ruby, you may wish to mention string interpolation.
– Sagar Pandya
Mar 27 at 17:08
2
Ruby will actually warn about "variable shadowing" which is using the same name twice in different, overlapping scopes, if the-w
flag is enabled. It's a good idea to avoid it not only because it's confusing, but because it can lead to unexpected results.
– tadman
Mar 27 at 17:21
add a comment |
You can use a different form of block like this:
puts "when are you born (year) ?"
birth_year = gets.to_i
birth_year.upto(2017) do |iterating_year|
puts "Year: #iterating_year"
puts "Age: #iterating_year - birth_year "
end
Or if you want it all in the same line try this - but it's less readable:
birth_year.upto(2017) iterating_year
Naming block variables
- Recommendation: Do not use the same variable name in your block as you have outside the block - that's just confusing. In this particular case, with the code as you have written it, the
birth_year
you have defined outside the block will have fallen out of scope, and the block parameter will take on the iterating years value: 2001, 2002, 2003 etc.
String Interpolation
This is when join strings together. One method you could use is to write some ruby code inside a string - you need a hash tag and opening and closing curly brackets to make it work. See this link for more info or google "ruby string interpoloation".
You can use a different form of block like this:
puts "when are you born (year) ?"
birth_year = gets.to_i
birth_year.upto(2017) do |iterating_year|
puts "Year: #iterating_year"
puts "Age: #iterating_year - birth_year "
end
Or if you want it all in the same line try this - but it's less readable:
birth_year.upto(2017) iterating_year
Naming block variables
- Recommendation: Do not use the same variable name in your block as you have outside the block - that's just confusing. In this particular case, with the code as you have written it, the
birth_year
you have defined outside the block will have fallen out of scope, and the block parameter will take on the iterating years value: 2001, 2002, 2003 etc.
String Interpolation
This is when join strings together. One method you could use is to write some ruby code inside a string - you need a hash tag and opening and closing curly brackets to make it work. See this link for more info or google "ruby string interpoloation".
edited Mar 27 at 18:23
answered Mar 27 at 16:54
BKSpurgeonBKSpurgeon
15.2k5 gold badges59 silver badges51 bronze badges
15.2k5 gold badges59 silver badges51 bronze badges
1
Since the OP appears to be new to Ruby, you may wish to mention string interpolation.
– Sagar Pandya
Mar 27 at 17:08
2
Ruby will actually warn about "variable shadowing" which is using the same name twice in different, overlapping scopes, if the-w
flag is enabled. It's a good idea to avoid it not only because it's confusing, but because it can lead to unexpected results.
– tadman
Mar 27 at 17:21
add a comment |
1
Since the OP appears to be new to Ruby, you may wish to mention string interpolation.
– Sagar Pandya
Mar 27 at 17:08
2
Ruby will actually warn about "variable shadowing" which is using the same name twice in different, overlapping scopes, if the-w
flag is enabled. It's a good idea to avoid it not only because it's confusing, but because it can lead to unexpected results.
– tadman
Mar 27 at 17:21
1
1
Since the OP appears to be new to Ruby, you may wish to mention string interpolation.
– Sagar Pandya
Mar 27 at 17:08
Since the OP appears to be new to Ruby, you may wish to mention string interpolation.
– Sagar Pandya
Mar 27 at 17:08
2
2
Ruby will actually warn about "variable shadowing" which is using the same name twice in different, overlapping scopes, if the
-w
flag is enabled. It's a good idea to avoid it not only because it's confusing, but because it can lead to unexpected results.– tadman
Mar 27 at 17:21
Ruby will actually warn about "variable shadowing" which is using the same name twice in different, overlapping scopes, if the
-w
flag is enabled. It's a good idea to avoid it not only because it's confusing, but because it can lead to unexpected results.– tadman
Mar 27 at 17:21
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55382431%2fshow-age-of-user%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Your block variable name is semantically incorrect. Rename it and then you'll easily see the correct calculation.
– Sagar Pandya
Mar 27 at 17:01
"Did not work" is not a useful diagnostic. Error messages, especially the exact text of those, is a lot better.
– tadman
Mar 27 at 17:20