diff --git a/README.md b/README.md index f0220db..2736d30 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ variety of languages and technologies. These are things that don't really warrant a full blog post. These are mostly things I learn by pairing with smart people at [Hashrocket](http://hashrocket.com/). -_427 TILs and counting..._ +_428 TILs and counting..._ --- @@ -333,6 +333,7 @@ _427 TILs and counting..._ - [Set RVM Default Ruby](ruby/set-rvm-default-ruby.md) - [Show Public Methods With Pry](ruby/show-public-methods-with-pry.md) - [Squeeze Out The Extra Space](ruby/squeeze-out-the-extra-space.md) +- [String Interpolation With Instance Variables](ruby/string-interpolation-with-instance-variables.md) - [Summing Collections](ruby/summing-collections.md) - [Uncaught Exceptions In Pry](ruby/uncaught-exceptions-in-pry.md) - [`undef_method` And The Inheritance Hierarchy](ruby/undef-method-and-the-inheritance-hierarchy.md) diff --git a/ruby/string-interpolation-with-instance-variables.md b/ruby/string-interpolation-with-instance-variables.md new file mode 100644 index 0000000..c575d7d --- /dev/null +++ b/ruby/string-interpolation-with-instance-variables.md @@ -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") +#=> # + +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