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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:42:48+00:00 2026-06-11T23:42:48+00:00

I was given this ruby code to overview. I am still new to ruby

  • 0

I was given this ruby code to overview. I am still new to ruby on rails. I come from a java background.

in User.rb:

  def last_name=name
    require 'debugger'; debugger
    self[:last_name] = name
  end

And told me that this is a setter method. They told me that this get executed in the “form” in this line:

<%= f.label :last_name%>
<%= f.text_field :last_name %>

Ok. Could somebody clarify how this ridiculous syntax can be valid?

1) An instance of the class “User” is never initialized. How is the method even called?

2) Where does the variable “name” comes from? what is the value of it? (the variable name is called nowhere else) And what does this syntax stand for? “def last_name=name” ?? Pass to the method a variable that has not been initialized? It is a short-cut for another syntax just to save typing 2 more symbols?

3) How can this method be called, in the form? I dont see a “User.last_name(“David”) or anything similar.

Could somebody clarify those piece of code please?
And please dont post links to tutorial or anything else. Just clarify this piece of code

  • 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-11T23:42:49+00:00Added an answer on June 11, 2026 at 11:42 pm

    The code you described:

    <%= f.label :last_name%>
    <%= f.text_field :last_name %>
    

    ..is used by the default rails template engine. It is view code.

    1) A User instance is likely initialized and populated when the form is submitted.

    The form action corresponds to an appropriate controller action, which likely accepts :last_name as a parameter. When you submit the form, the controller action probably instantiates the User instance. Without more code, however, I can’t be 100% certain this is the case with your application.

    2) The variable name comes from the argument accepted by the last_name method.

    Perhaps, to help you understand the method, let’s rewrite it:

    def last_name= (name)
      require 'debugger'; debugger
      self[:last_name] = name
    end
    

    Either last_name=('John Doe') or last_name = 'John Doe' will execute this method.

    3) I think my previous descriptions should help you make sense of this..

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

Sidebar

Related Questions

Given this code: List<Integer> ints = new ArrayList<Integer>(); // Type mismatch: // cannot convert
I am relatively new to ruby on rails, so this question might be easy.
Given the ruby code: aaaa\nbbbb\n\n.split(/\n/) This outputs: [aaaa, bbbb] I would like the output
Given: A rails 2.3.x application currently being upgraded to rails 3. Upgrading ruby from
Given this hash in ruby: h={ 2010-03-01=>2 2010-03-02=>4 2010=03-03=>1 .. .. n days with
given an xml string like this: <some><nested><xml>value</xml></nested></some> what's the best option (using ruby) to
Given this JavaScript code (which is just a comment referring to a url): //
Given the following Ruby code, and given I have an instance of Klass, how
Given the following code, How would you refactor this so that the method search_word
I wrote some code that looks like this: def get(x, y) @cachedResults.set(x,y, Math.hypot(x, y))

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.