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 … Read more

Tell Ruby Program to Wait some amount of time

Like this: The num_secs value can be an integer or float. Also, if you’re writing this within a Rails app, or have included the ActiveSupport library in your project, you can construct longer intervals using the following convenience syntax:

Parsing a JSON string in Ruby

This looks like JavaScript Object Notation (JSON). You can parse JSON that resides in some variable, e.g. json_string, like so: If you’re using an older Ruby, you may need to install the json gem. There are also other implementations of JSON for Ruby that may fit some use-cases better: YAJL C Bindings for Ruby JSON::Stream

How to write a switch statement in Ruby

Ruby uses the case expression instead. Ruby compares the object in the when clause with the object in the case clause using the === operator. For example, 1..5 === x, and not x === 1..5. This allows for sophisticated when clauses as seen above. Ranges, classes and all sorts of things can be tested for rather than just equality. Unlike switch statements in many other languages, Ruby’s case does not have fall-through, so … Read more

What does the “map” method do in Ruby?

The map method takes an enumerable object and a block, and runs the block for each element, outputting each returned value from the block (the original object is unchanged unless you use map!): Array and Range are enumerable types. map with a block returns an Array. map! mutates the original array. Where is this helpful, and what is the difference between map! and each? Here is an example: … Read more

Why does Ruby on Rails use http://0.0.0.0:3000 instead of http://localhost:3000?

Localhost means quite literally “your local host”, usually identified by 127.0.0.1 and all traffic to that address is routed via a loopback interface. If your Web server is listening for connections on 127.0.0.1, this means that it only accepts requests coming from the same host. 0.0.0.0 means that Rails is listening on all interfaces, not just the … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)