mirror of
https://github.com/jbranchaud/til
synced 2026-01-04 23:58:01 +00:00
Add String Interpolation With Instance Variables as a ruby til
This commit is contained in:
31
ruby/string-interpolation-with-instance-variables.md
Normal file
31
ruby/string-interpolation-with-instance-variables.md
Normal file
@@ -0,0 +1,31 @@
|
||||
# String Interpolation With Instance Variables
|
||||
|
||||
When using regular variables with string interpolation in Ruby, they must be
|
||||
wrapped in curly braces (e.g. `"This is a #{variable}"`). With instance
|
||||
variables (and class and global variables) you can just use the _octothorp_
|
||||
followed directly by the variable.
|
||||
|
||||
Here is an example of this in action:
|
||||
|
||||
```ruby
|
||||
class Person
|
||||
def initialize(name)
|
||||
@name = name
|
||||
end
|
||||
|
||||
def whoami
|
||||
puts "I am #@name"
|
||||
end
|
||||
end
|
||||
|
||||
bob = Person.new("bob")
|
||||
#=> #<Person:0x007fdaf3291618 @name="bob">
|
||||
|
||||
bob.whoami
|
||||
# I am bob
|
||||
```
|
||||
|
||||
This is a handy shortcut, but may affect readability and/or result in an
|
||||
interpolation error at some point. Your mileage may vary.
|
||||
|
||||
h/t Josh Davey
|
||||
Reference in New Issue
Block a user