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;








1















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.










share|improve this question


























  • 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

















1















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.










share|improve this question


























  • 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













1












1








1








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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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

















  • 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












1 Answer
1






active

oldest

votes


















3















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.

Same and confusing - avoid



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".






share|improve this answer






















  • 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










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
);



);













draft saved

draft discarded


















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









3















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.

Same and confusing - avoid



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".






share|improve this answer






















  • 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















3















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.

Same and confusing - avoid



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".






share|improve this answer






















  • 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













3














3










3









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.

Same and confusing - avoid



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".






share|improve this answer















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.

Same and confusing - avoid



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".







share|improve this answer














share|improve this answer



share|improve this answer








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












  • 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








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.



















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript