Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9058625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T14:46:14+00:00 2026-06-16T14:46:14+00:00

Consider the following two peices of ruby code Example 1 name = user.first_name round_number

  • 0

Consider the following two peices of ruby code

Example 1

name = user.first_name
round_number = rounds.count
users.each do |u|
  puts "#{name} beat #{u.first_name} in round #{round_number}"
end

Example 2

users.each do |u|
  puts "#{user.first_name} beat #{u.first_name} in #{rounds.count}"
end

For both pieces of code imagine

#user.rb
def first_name
  name.split.first
end

So in a classical analysis of algorithms, the first piece of code would be more efficient, however in most modern compiled languages, modern compilers would optimize the second piece of code to make it look like the first, eliminating the need to optimize code in such maner.

Will ruby optimize or cache values for this code before execution? Should my ruby code look like example 1 or example 2?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T14:46:16+00:00Added an answer on June 16, 2026 at 2:46 pm

    Example 1 will run faster, as first_name() is only called once, and it’s value stored in the variable.

    In Example 2 Ruby will not memoize this value automatically, since the value could have changed between iterations for the each() loop.

    Therefor expensive-to-calculate methods should be explicitly memoized if they are expected to be used more than once without the return value changing.

    Making use of Ruby’s Benchmark Module can be useful when making decisions like this. It will likely only be worth memoizing if there are a lot of values in users, or if first_name() is expensive to calculate.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following two code examples: Example 1. public void setValue(int value) { mValue
consider the following two pieces of code: public static Time Parse(string value) { string
Consider the following two pieces of code: var adj=0>grip.y?0<grip.x?0:-180:0<grip.x?-360:-180; and var adj; if (grip.y
Consider two following pieces of code - the only difference between them is a
Consider the following two segments of code in Java, Integer x=new Integer(100); Integer y=x;
Consider the following two methods, written in pseudo code, that fetches a complex data
Consider the following two code samples: NSData *imgData = UIImagePNGRepresentation(imgFull); NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
Consider the following two lines of code if (test ! = null) and if
Consider the following two fragments of code: scala> def f1(x:Any) = x match {
Consider the following two relations: @Entity class Foo { @Id id; @ManyToMany @JoinTable(name =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.