concept name in category ruby

appears as: Names, names, name, name, The names, The name
The Well-Grounded Rubyist, Third Edition

This is an excerpt from Manning's book The Well-Grounded Rubyist, Third Edition.

$ ruby -e 'print "Enter a name: "
puts gets.reverse'
Enter a name: David A. Black
kcalB .A divaD

On the other hand, you need to be cautious about modifying objects in place, because other parts of the program may depend on those objects not to change. For example, let’s say you have a database of names. You read the names out of the database into an array. At some point, you need to process the names for printed output—all in capital letters. You may do something like this:

names.each do |name|
  capped = name.upcase
  # ...code that does something with capped...
end

In this example, capped is a new object—an uppercase duplicate of name. When you go through the same array later, in a situation where you do not want the names in uppercase, such as saving them back to the database, the names will be the way they were originally.

By creating a new string (capped) to represent the uppercase version of each name, you avoid the side effect of changing the names permanently. The operation you perform on the names achieves its goals without changing the basic state of the data. Sometimes you’ll want to change an object permanently, and sometimes you won’t want to. There’s nothing wrong with that, as long as you know which you’re doing and why.

This chapter covered several topics that pertain to multiple built-in classes and modules. You’ve seen Ruby’s literal constructors, which provide a concise alternative to calling new on certain built-in classes. You’ve also seen how Ruby provides syntactic sugar for particular method names, including a large number of methods with names that correspond to arithmetic operators.

Rails 4 in Action: Revised Edition of Rails 3 in Action

This is an excerpt from Manning's book Rails 4 in Action: Revised Edition of Rails 3 in Action.

Now that Rails is installed, to generate an application, you run the rails command and pass it the new argument and the name of the application you want to generate: things_i_bought. When you run this command, it creates a new directory called things_i_bought, which is where all your application’s code will go.

To define a validation to ensure that the name attribute is provided when a project is created, open the app/models/project.rb file and make it look like the following listing.

Listing 3.23. app/models/project.rb
class Project < ActiveRecord::Base validates :name, presence: true end

The validates method’s usage is the same as how you used it in chapter 1. It tells the model that you want to validate the name field, and that you want to validate its presence. There are other kinds of validations as well; for example, the :uniqueness key, when passed true as the value, validates the uniqueness of this field as well, ensuring that only one record in the table has that specific value.

irb(main):001:0> Project.create!
ActiveRecord::RecordInvalid: Validation failed: Name can't be blank

In the projects migration, the first line of the change method tells Active Record that you want to create a table called projects. You call this method in the block format, which returns an object that defines the table. To add fields to this table, you call methods on the block’s object (called t in this example and in all model migrations), the name of which usually reflects the type of column it is; the first argument is the name of that field. The timestamps method is special: it creates two fields, created_at and updated_at, which are by default set to the current time in coordinated universal time (UTC)[8] by Rails when a record is created and updated, respectively.

Rails 3 in Action

This is an excerpt from Manning's book Rails 3 in Action.

The scaffold command generates a model, a controller, and views based on the name passed after scaffold in this command. These are the three important parts needed for your purchase tracking. The model provides a way to interact with a database. The controller interacts with the model to retrieve and format its information and defines different actions to perform on this data. The views display the information from the controller in a neat format.

Everything after the name for the scaffold are the fields for the database table and the attributes for the objects of this scaffold. Here you tell Rails that the table for your purchase scaffold will contain name and cost fields, which are a string and a float.[9] To create this table, the scaffold generator generates what’s known as a migration. Let’s have a look at what migrations are.

Listing 3.25. features/creating_projects.feature
Scenario: Creating a project without a name Given I am on the homepage When I follow "New Project" And I press "Create Project" Then I should see "Project has not been created." And I should see "Name can't be blank"
Flexible Rails: Flex 3 on Rails 2

This is an excerpt from Manning's book Flexible Rails: Flex 3 on Rails 2.

Next, in Flex Builder, choose File > New > Flex Project. Give the project the name pomodo, uncheck the Use Default Location check box, and click Browse to browse to the location where you ran the rails pomodo command and then into the pomodo directory that was created by the rails pomodo command (for me, this is C:\peter\flexiblerails\current\pomodo). Leave Application Type set to Web Application (Runs in Flash Player), and leave Application Server Type set to None. The dialog now looks like the one shown in figure 2.3.

We could say user.child('first-name') in the MainBox, and it would work. (Try it.) But we can’t say user.first-name, because Flex will think we’re subtracting some variable called name from user.first. We’re stubborn, and we like the sexy E4X (ECMAScript for XML) syntax (such as user.first_name), so we’re not going to give in and use user.child('first-name'). Besides, not knowing where the hyphen came from is unsettling. (Another workaround would be to not have underscores in our table column names. However, this smells like accepting defeat too easily—and by this point, this is making us mad.) Also, trying to figure this out should be instructive.

mysql> select id, user_id, name, notes, created_at from locations;
+----+---------+----------+----------+---------------------+
| id | user_id | name     | notes    | created_at          |
+----+---------+----------+----------+---------------------+
|  1 |       1 | MyString | MyText   | 2007-10-19 12:27:11 |
|  2 |       1 | MyString | MyText   | 2007-10-19 12:27:11 |
|  3 |       3 | Vivoli   | Florence | 2007-10-20 20:25:50 |
+----+---------+----------+----------+---------------------+
3 rows in set (0.00 sec)

mysql>