Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does ?/ mean in Ruby? [duplicate]

Tags:

string

ruby

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?

like image 722
Simone Avatar asked Mar 06 '14 13:03

Simone


People also ask

What does DUP do in Ruby?

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.

What does '?' Mean in Ruby?

i know that the '?' in ruby is something that checks the yes/no fulfillment.

What is DUP in Ruby on Rails?

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.

What does .first mean in Ruby?

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.

What are Ruby symbols&how do they work?

- 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.

What are the operators in Ruby?

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.

What does truthy mean in Ruby?

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.

What is the difference between Ruby symbols&strings?

… 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.


1 Answers

? 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"
like image 160
Arup Rakshit Avatar answered Oct 11 '22 03:10

Arup Rakshit