I just stumbled across this piece of code:
if source[0] != ?/
source = compute_asset_path(source, options)
end
What's this "?/
"? I've never seen writing strings this way.
$ irb
2.0.0p247 :001 > ?/
=> "/"
Apparently it works for single characters only:
2.0.0p247 :001 > ?a
=> "a"
2.0.0p247 :002 > ?foo
SyntaxError: (irb):2: syntax error, unexpected '?'
What does ?
mean?
According to Ruby-doc, both #clone and #dup can be used to create a shallow copy of an object, which only traverse one layer of complexity, meaning that the instance variables of obj are copied, but not the objects they reference. They would all share the same attributes; modifying one would result a change on another.
i know that the '?' in ruby is something that checks the yes/no fulfillment.
This is what Rails is using with its #dup method on ActiveRecord. It uses #dup to allow you to duplicate a record without its "internal" state (id and timestamps), and leaves #clone up to Ruby to implement. Having this extra method also asks for a specific initializer when using the #clone method.
The first() is an inbuilt method in Ruby returns an array of first X elements. If X is not mentioned, it returns the first element only. Syntax: range1.first(X) Parameters: The function accepts X which is the number of elements from the beginning. Return Value: It returns an array of first X elements.
- RubyGuides What Are Ruby Symbols & How Do They Work? A symbol looks like this: Some people confuse symbols with variables, but they have nothing to do with variables… … a symbol is a lot more like a string. So what are the differences between Ruby symbols & strings? Strings are used to work with data. Symbols are identifiers.
Ruby supports a rich set of operators, as you'd expect from a modern language. Most operators are actually method calls. For example, a + b is interpreted as a.+(b), where the + method in the object referred to by variable a is called with b as its argument.
Truthy A value that is considered true when evaluated in a boolean context. In Ruby, everything is truthy, except nil & false. Test-Driven Development A programming technique where you write a failing test before the actual code & then write just as little code as possible to make the test pass.
… a symbol is a lot more like a string. So what are the differences between Ruby symbols & strings? Strings are used to work with data. Symbols are identifiers. That’s the main difference: Symbols are not just another kind of string, they have a different purpose.
?
is used to represent single character string literals. Like ?a
,?b
but not ?ab
.
To answer the comment of OP :
Yes, they are.
irb(main):001:0> ?x + 'y'
=> "xy"
irb(main):002:0> 'x' + 'y'
=> "xy"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With