What Does the "$" Character Mean in Ruby

What does the $ character mean in Ruby?

$: is the global variable used for looking up external files.

From http://www.zenspider.com/Languages/Ruby/QuickRef.html#18

$: Load path for scripts and binary modules by load or require.

What is the comma character meaning in right side of expression in Ruby

It's two symbols separated by a comma, and is an implicit array.

Equivalent to

self.primary_keys = [:role_id, :action_name]

It's more common to see the technique used on the left side of an assignment.

name, age = ["George", 21]

puts name
=> "George"

puts age
=> 21

The feature lets you swap the contents of variables without an intermediate variable.

For example, in some languages to swap a and b you need a temporary variable

temporary = a
a = b
b = temporary

In Ruby you can do

a, b = b, a

@ variables in Ruby on Rails

title is a local variable. They only exists within its scope (current block)

@title is an instance variable - and is available to all methods within the class.

You can read more here:
http://strugglingwithruby.blogspot.dk/2010/03/variables.html

In Ruby on Rails - declaring your variables in your controller as instance variables (@title) makes them available to your view.

What does ?/ mean in Ruby?

? 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"

How to generate a random string in Ruby

(0...8).map { (65 + rand(26)).chr }.join

I spend too much time golfing.

(0...50).map { ('a'..'z').to_a[rand(26)] }.join

And a last one that's even more confusing, but more flexible and wastes fewer cycles:

o = [('a'..'z'), ('A'..'Z')].map(&:to_a).flatten
string = (0...50).map { o[rand(o.length)] }.join

If you want to generate some random text then use the following:

50.times.map { (0...(rand(10))).map { ('a'..'z').to_a[rand(26)] }.join }.join(" ")

this code generates 50 random word string with words length less than 10 characters and then join with space

What do these weird characters mean?

  1. validates is a method, part of the validators in Rails. It is declared in (actually, included to) a superclass, that is why it does not have to be declared in the model. The : in front of anything signifies a symbol, not a variable. Symbols are part of Ruby, somewhat similar to strings.
  2. form_for is a method, which takes a number of parameters and a block (that is why there is a do afterwards). The | is part of Ruby syntax, the way you enclose code block parameters.
  3. edit_post_path is defined by the Rails magic and the routes. It is a helper method.

I encourage you to read this book about Ruby to get more familiar with symbols, code blocks, modules and other things that make Ruby a great programming language.

What does the unary question mark (?) operator do?

It returns a single character string. It is the shortest way to write a single-character string literal. Use it when you want to define a lot of single-character strings. It is a heritage from Ruby <1.9, where it used to return the ASCII code for that character. I don't understand what you mean by "break the language orthogonality".

Is 'c' said to be a character or a string in Ruby - or both?

In C, a character is distinct from a string (which is an array of characters). Ruby does not have an individual character type. Strings can hold any number of characters, and Fixnums can hold the ASCII value for a character and be converted to a printable string containing that character with the #chr method.

The difference between the single-quote and double-quote string syntax in Ruby has to do with how much preprocessing (interpolation, for example) is done on the string.



Related Topics



Leave a reply



Submit