简体   繁体   English

什么是def to_s功能?

[英]What is the def to_s function?

I'm going through the "Tags" section of the Blogger tutorial and am a bit confused on one part: the def to_s function (in tag.rb); 我正在阅读Blogger教程的“标签”部分,我对一部分感到困惑:def to_s函数(在tag.rb中); why it's required and how it's included. 为什么需要它以及如何包含它。

I've included some relevant parts of relevant files for context. 我已经为相关文件中包含了相关文件的一些相关部分。

MODELS 楷模

article.rb article.rb

class Article < ActiveRecord::Base
  attr_accessible :tag_list
  has_many :taggings
  has_many :tags, through: :taggings

  def tag_list
    return self.tags.collect do |tag|
      tag.name
    end.join(", ")
  end

  def tag_list=(tags_string)
    self.taggings.destroy_all

      tag_names = tags_string.split(",").collect{|s| s.strip.downcase}.uniq

      tag_names.each do |tag_name|
        tag = Tag.find_or_create_by_name(tag_name)
        tagging = self.taggings.new
        tagging.tag_id = tag.id
      end
    end
  end

tag.rb tag.rb

class Tag < ActiveRecord::Base
  has_many :taggings
  has_many :articles, through: :taggings

  def to_s
    name
  end
end

tagging.rb tagging.rb

class Tagging < ActiveRecord::Base
  belongs_to :tag
  belongs_to :article
end

CONTROLLERS CONTROLLERS

tags_controller.rb tags_controller.rb

class TagsController < ApplicationController

  def index
    @tags = Tag.all
  end

  def show
    @tag = Tag.find(params[:id])
  end

  def destroy
    @tag = Tag.find(params[:id]).destroy
    redirect_to :back
  end
end

HELPERS 助手

articles_helper.rb articles_helper.rb

module ArticlesHelper

  def tag_links(tags)
    links = tags.collect{|tag| link_to tag.name, tag_path(tag)}
    return links.join(", ").html_safe
  end
end

VIEWS VIEWS

new.html.erb new.html.erb

<%= form_for(@article, html: {multipart: true}) do |f| %>
  <p>
    <%= f.label :tag_list %>
    <%= f.text_field :tag_list %>
  </p>
  <p>
    <%= f.submit %>
  </p>
<% end %>

show.html.erb show.html.erb

Tags: <%= tag_links(@article.tags) %> 标签: <%= tag_links(@article.tags) %>

I got your point. 我明白了你的意思。 When you concatenate value in string you have to write eg 当您在字符串中连接值时,您必须编写例如

"hello #{@user.name}" 

So instead of calling @user.name u can specify whatever u have to display user as a string you can directly specify in to_s method so that you dont need to call .to_s again just write 因此,不是调用@ user.name你可以指定任何你必须显示用户的字符串,你可以直接在to_s方法中指定,这样你就不需要再次调用.to_s了

"hello #{@user}"

above line of code search for .to_s method for @user's class and print returning value. 上面的代码行搜索@ user的类和打印返回值的.to_s方法。

same is for routing like 同样适用于路由

user_path(@user)

will give you >> users/123 # where 123 is id of @user 会给你>> users / 123 #,其中123是@user的id

if you write 如果你写

def to_params
 self.name
end

Then it will give >> users/john # where john is the @user's name 然后它会给>> users / john #,其中john是@用户的名字

to_s is the standard Ruby method for converting an Object into a string. to_s是将Object转换为字符串的标准Ruby方法。 You define to_s when you want a custom string representation for your class. 您希望为类创建自定义字符串表示时定义to_s Example: 例:

1.to_s #=> "1"
StandardError.new("ERROR!").to_s #=> "ERROR!"

And so on. 等等。

to_s is a function that returns the string equivalent of the object. to_s是一个返回对象的字符串等效函数。

In this case, you have Tag.to_s defined, which allows you to do something like this 在这种情况下,您已经定义了Tag.to_s ,它允许您执行类似的操作

tag = Tag.new(:name => "SomeTag")
tag.to_s #=> "SomeTag"

You can add more logic to to_s to have a more friendly string, rather than an object hashcode when you want string from a tag (ex. while printing to console). 您可以向to_s添加更多逻辑以获得更友好的字符串,而不是当您需要来自标记的字符串时的对象哈希码(例如,在打印到控制台时)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM