Skip to content

String literal #15

@byhbt

Description

@byhbt

Problem:

Bit confuse about String Literal in Ruby.

Solution:

What is String?
Object in Ruby.

What is Literal?
The value appears directly in Ruby code.

Example:

╰─$ irb
2.4.0 :005 > 5.times { a = 'test'; puts a.object_id; }
70105430666100
70105430665960
70105430665840
70105430665760
70105430665700
 => 5
2.4.0 :006 > 5.times { a = 'test'.freeze; puts a.object_id }
70105426108320
70105426108320
70105426108320
70105426108320
70105426108320
 => 5

Lesson:

According to The Ruby Programming Language book:

For efficiency, you should avoid using literals within loops.

Metadata

Metadata

Assignees

No one assigned

    Labels

    RubyLearning Ruby on Rails

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions