concept behavior in category ruby

appears as: behavior, behaviors, behavior, behaviors, The behavior
The Well-Grounded Rubyist, Third Edition

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

You can also write your own classes. You can even modify existing Ruby classes; if you don’t like the behavior of strings or arrays, you can change it. It’s almost always a bad idea to do so, but Ruby allows it. (We’ll look at the pros and cons of making changes to built-in classes in chapter 13.)

Although every Ruby object is an instance of a class, the concept of class is less important than the concept of object. That’s because objects can change, acquiring methods and behaviors that weren’t defined in their class. The class is responsible for launching the object into existence, a process known as instantiation, but thereafter the object has a life of its own.

The ability of objects to adopt behaviors that their class didn’t give them is one of the most central defining principles of the design of Ruby as a language. As you can surmise, we’ll come back to it frequently in a variety of contexts. For now, just be aware that although every object has a class, the class of an object isn’t the sole determinant of what the object can do.

Ruby objects are often (perhaps most often) handled via variables that represent them, and in this chapter, we’ll get deeper technically than we have so far into the nature and behavior of variables in Ruby. Once you’ve worked through this chapter, you’ll have a firm foothold in the landscape of Ruby objects and their manipulation.

At first, the concept of OOP tends to come across as both simple (you write programs that have books and bottles and cars and houses, and you orchestrate a kind of conversation among those things) and abstract (Object? What does that mean? What do I type into my program file to create a “house” object?). OOP does have a component of simplicity; it lets you draw on objects, entities, roles, and behaviors as a source for how you design your programs, and that can be a help. At the same time, to create and use objects in your programs, you have to learn how it’s done in a given language.

Listing 4.1. Stacklike module, encapsulating stacklike structure and behavior
module Stacklike
  def stack
    @stack ||= []         #1
  end
  def add_to_stack(obj)   #2
    stack.push(obj)
  end
  def take_from_stack     #3
    stack.pop
  end
end


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":2,\"ch\":26},{\"line\":2,\"ch\":28}],[{\"line\":4,\"ch\":26},{\"line\":4,\"ch\":28}],[{\"line\":7,\"ch\":26},{\"line\":7,\"ch\":28}]]"}
!@%STYLE%@!
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.

You use RSpec.describe to describe the behavior of the (currently undefined) Bacon class and write an example for it, declaring that Bacon is edible. The describe block contains tests (examples) that describe the behavior of bacon. In this example, whenever you call edible? on Bacon, the result should be true. expect and to serve a purpose similar to that of assert, which is to assert that the object passed to expect matches the arguments passed to to. If the outcome isn’t what you say it should be, then RSpec raises an error and goes no further with that spec.

Listing 10.28. Configuring the behavior on unpermitted params
Rails.application.configure do ... config.action_controller.action_on_unpermitted_parameters = :raise end