File tree Expand file tree Collapse file tree 2 files changed +33
-1
lines changed Expand file tree Collapse file tree 2 files changed +33
-1
lines changed Original file line number Diff line number Diff line change @@ -7,7 +7,7 @@ variety of languages and technologies. These are things that don't really
7
7
warrant a full blog post. These are mostly things I learn by pairing with
8
8
smart people at [ Hashrocket] ( http://hashrocket.com/ ) .
9
9
10
- _ 427 TILs and counting..._
10
+ _ 428 TILs and counting..._
11
11
12
12
---
13
13
@@ -333,6 +333,7 @@ _427 TILs and counting..._
333
333
- [ Set RVM Default Ruby] ( ruby/set-rvm-default-ruby.md )
334
334
- [ Show Public Methods With Pry] ( ruby/show-public-methods-with-pry.md )
335
335
- [ Squeeze Out The Extra Space] ( ruby/squeeze-out-the-extra-space.md )
336
+ - [ String Interpolation With Instance Variables] ( ruby/string-interpolation-with-instance-variables.md )
336
337
- [ Summing Collections] ( ruby/summing-collections.md )
337
338
- [ Uncaught Exceptions In Pry] ( ruby/uncaught-exceptions-in-pry.md )
338
339
- [ ` undef_method ` And The Inheritance Hierarchy] ( ruby/undef-method-and-the-inheritance-hierarchy.md )
Original file line number Diff line number Diff line change
1
+ # String Interpolation With Instance Variables
2
+
3
+ When using regular variables with string interpolation in Ruby, they must be
4
+ wrapped in curly braces (e.g. ` "This is a #{variable}" ` ). With instance
5
+ variables (and class and global variables) you can just use the _ octothorp_
6
+ followed directly by the variable.
7
+
8
+ Here is an example of this in action:
9
+
10
+ ``` ruby
11
+ class Person
12
+ def initialize (name )
13
+ @name = name
14
+ end
15
+
16
+ def whoami
17
+ puts " I am #@name "
18
+ end
19
+ end
20
+
21
+ bob = Person .new (" bob" )
22
+ # => #<Person:0x007fdaf3291618 @name="bob">
23
+
24
+ bob.whoami
25
+ # I am bob
26
+ ```
27
+
28
+ This is a handy shortcut, but may affect readability and/or result in an
29
+ interpolation error at some point. Your mileage may vary.
30
+
31
+ h/t Josh Davey
You can’t perform that action at this time.
0 commit comments