Meaning of #{ } in Ruby

Meaning of #{ } in Ruby?


Why This Is a Good Question

This is a tough question to Google for unless you know the right search terms. The #{} operator technically performs expression substitution inside a string literal.

The Answer

The #{} literal is the operator used for interpolation inside double-quoted strings the same way that the backticks or $() construct would be used in Bash. From a practical point of view, the expression inside the literal is evaluated, and then the entire #{} expression (including both the operator and the expression it contains) is replaced in situ with the result.

Related Links

  • http://www.ruby-doc.org/docs/ruby-doc-bundle/Manual/man-1.4/syntax.html#string
  • http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html
  • http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Literals#Interpolation
  • Ruby (on Rails) syntax

what does ? ? mean in ruby

Functions that end with ? in Ruby are functions that only return a boolean, that is, true, or false.

When you write a function that can only return true or false, you should end the function name with a question mark.

The example you gave shows a ternary statement, which is a one-line if-statement. .nil? is a boolean function that returns true if the value is nil and false if it is not. It first checks if the function is true, or false. Then performs an if/else to assign the value (if the .nil? function returns true, it gets nil as value, else it gets the File.join(root_dir, '/') as value.

It can be rewritten like so:

if root_dir.nil?
prefix = nil
else
prefix = File.join(root_dir, '/')
end

What does ||= (or-equals) mean in Ruby?

This question has been discussed so often on the Ruby mailing-lists and Ruby blogs that there are now even threads on the Ruby mailing-list whose only purpose is to collect links to all the other threads on the Ruby mailing-list that discuss this issue.

Here's one: The definitive list of ||= (OR Equal) threads and pages

If you really want to know what is going on, take a look at Section 11.4.2.3 "Abbreviated assignments" of the Ruby Language Draft Specification.

As a first approximation,

a ||= b

is equivalent to

a || a = b

and not equivalent to

a = a || b

However, that is only a first approximation, especially if a is undefined. The semantics also differ depending on whether it is a simple variable assignment, a method assignment or an indexing assignment:

a    ||= b
a.c ||= b
a[c] ||= b

are all treated differently.

What does #{ } mean in Ruby?

It is used for String interpolation: ( wikipedia, ctrl+f "ruby" )

apples = 4
puts "I have #{apples} apples"
# or
puts "I have %s apples" % apples
# or
puts "I have %{a} apples" % {a: apples}

The output will be:

I have 4 apples

String interpolation, Definition:

In Ruby, string interpolation refers to the ability of double-quoted strings to execute Ruby code and replace portions of that strings (denoted by #{ ... }) with the evaluation of that Ruby code.

It is the most common way to inject data (usually the value of a variable, but it can be the evaluation of any Ruby code) into the middle of a string.


A thing to know:

puts "This User name is #{User.create(username: 'Bobby')}!"

This will make an implicit call of .to_s on the User's instance object.

If you defined the method .to_s on the User model:

class User
def to_s
self.username
end

It would output:

puts "This User name is #{User.create(username: 'Bobby')}"
# => "This User name is Bobby"

What do =~ and /\ mean in Ruby?

=~ is known as the "match operator" and can be used to match a string against a regular expression.

The /\ is actually part of two separate things. / denotes the start of a regular expression and \A is known as an "anchor" and is saying "match from the beginning of the string."

edit: This is a link to the documentation that should help you understand more code like you posted.

thank you to Wayne Conrad for a correction on '/\'

What does $/ mean in Ruby?

$/ is a pre-defined variable. It's used as the input record separator, and has a default value of "\n".

Functions like gets uses $/ to determine how to separate the input. For example:

$/="\n\n"
str = gets
puts str

So you have to enter ENTER twice to end the input for str.

Reference: Pre-defined variables

What is the meaning of %%= in Ruby on Rails?

In short, ERb processes double-percent marks into single-percent marks.


It looks like you're using one layer of ERb templates to generate another layer of ERb templates.

The first layer of ERb doesn't need a variable called content, just the t method:

<span class="option-content" placeholder="<%=t('pages.edit.option')%>">
<%%= content %>
</span>

That first layer is rendered to produce the second layer:

<span class="option-content" placeholder="Edit">
<%= content %>
</span>

As you can see, that is also an ERb template. I expect that something else, later on, takes that second ERb and uses it to render something like:

<span class="option-content" placeholder="Edit">
Hello, world.
</span>

In Ruby what does = mean and how does it work?

=> separates the keys from the values in a hashmap literal. It is not overloadable and not specifically connected to symbols.

A hashmap literal has the form {key1 => value1, key2 => value2, ...}, but when used as the last parameter of a function, you can leave off the curly braces. So when you see a function call like f(:a => 1, :b => 2), f is called with one argument, which is a hashmap that has the keys :a and :b and the values 1 and 2.

What does ||= mean?

Basically, a ||= b means assign b to a if a is null or undefined or false (i.e. false-ish value in ruby), it is similar to a = b unless a, except it will always evaluate to the final value of a (whereas a = b unless a would result in nil if a was true-ish).



Related Topics



Leave a reply



Submit