Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String literal #15

Open
byhbt opened this issue Aug 25, 2020 · 0 comments
Open

String literal #15

byhbt opened this issue Aug 25, 2020 · 0 comments
Labels
Ruby Learning Ruby on Rails

Comments

@byhbt
Copy link
Owner

byhbt commented Aug 25, 2020

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.
@byhbt byhbt added the Ruby Learning Ruby on Rails label Aug 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Ruby Learning Ruby on Rails
Projects
None yet
Development

No branches or pull requests

1 participant