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 7034827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:13:57+00:00 2026-05-28T01:13:57+00:00

My background is in PHP and C#, but I’d really like to learn RoR.

  • 0

My background is in PHP and C#, but I’d really like to learn RoR. To that end, I’ve started reading the official documentation. I have some questions about some code examples.

The first is with iterators:

class Array
  def inject(n)
    each { |value| n = yield(n, value) }
    n
  end

  def sum
    inject(0) { |n, value| n + value }
  end

  def product
    inject(1) { |n, value| n * value }
  end
end

I understand that yield means “execute the associated block here.” What’s throwing me is the |value| n = part of the each. The other blocks make more sense to me as they seem to mimic C# style lambdas:

public int sum(int n, int value)
{
    return Inject((n, value) => n + value);
}

But the first example is confusing to me.

The other is with symbols. When would I want to use them? And why can’t I do something like:

class Example
  attr_reader @member

  # more code
end
  • 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-05-28T01:13:57+00:00Added an answer on May 28, 2026 at 1:13 am

    In the inject or reduce method, n represents an accumulated value; this means the result of every iteration is accumulated in the n variable. This could be, as is in your example, the sum or product of the elements in the array.

    yield returns the result of the block, which is stored in n and used in the next iterations. This is what makes the result “cumulative.”

    a = [ 1, 2, 3 ]
    a.sum  # inject(0) { |n, v| n + v }
    # n == 0; n = 0 + 1
    # n == 1; n = 1 + 2
    # n == 3; n = 3 + 3
     => 6
    

    Also, to compute the sum you could also have written a.reduce :+. This works for any binary operation. If your method is named symbol, writing a.reduce :symbol is the same as writing a.reduce { |n, v| n.symbol v }.

    attr and company are actually methods. Under the hood, they dynamically define the methods for you. It uses the symbol you passed to work out the names of the instance variable and the methods. :member results in the @member instance variable and the member and member = methods.

    The reason you can’t write attr_reader @member is because @member isn’t an object in itself, nor can it be converted to a symbol; it actually tells ruby to fetch the value of the instance variable @member of the self object, which, at class scope, is the class itself.

    To illustrate:

    class Example
      @member = :member
      attr_accessor @member
    end
    
    e = Example.new
    e.member = :value
    e.member
     => :value
    

    Remember that accessing unset instance variables yields nil, and since the attr method family accepts only symbols, you get: TypeError: nil is not a symbol.

    Regarding Symbol usage, you can sort of use them like strings. They make excellent hash keys because equal symbols always refer to the same object, unlike strings.

    :a.object_id == :a.object_id
     => true
    'a'.object_id == 'a'.object_id
     => false
    

    They’re also commonly used to refer to method names, and can actually be converted to Procs, which can be passed to methods. This is what allows us to write things like array.map &:to_s.

    Check out this article for more interpretations of the symbol.

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

Sidebar

Related Questions

I've got a PHP background, but I'm beginning to learn Objective-C, so that I
im using php to include background colors, so that users can change them, but
I'm from a PHP background but have been given a Django project to administer,
I primarily come from a PHP background but I have been asked to develop
I'm from a Java/PHP background and I'm trying to learn Rails, but I'm finding
Coming from a PHP background, I'm used to writing small functions that return a
if i have a php background job, may need to run 4-5hours or more
I,m trying to run one php script in background. But its no working... I
I have a very similar setup to the person here: PHP Background Processes i.e
I recently started development in ASP.NET, and coming from a PHP background I sprinkle

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.