How to Understand Sender and Receiver in Ruby

How to understand sender and receiver in Ruby?

A core concept in Object Orientation is messaging and early conceptualization borrowed much from the Actor Model of computation. Alan Kay, the guy who coined the term Object Oriented and invented one of the first OO languages SmallTalk, has voiced regret at using a term which put the focus on objects instead of on messages, which he considered the stronger idea.

When talking about a message, there's a natural "sender" and "receiver" of the message. The sender is the object which invokes a method, the receiver is the object whose method is invoked. In Ruby, if one calls a method without explicitly naming an object, that sends the method name and its args as a message to the default receiver self.

In OO, "making a call", "invoking a method", and "sending a message" are equivalent concepts. Similarly "being called", "having one's method invoked", and "receiving a message" are equivalent.

In Ruby what does the receiver refer to?

In Ruby (and other languages that take inspiration from SmallTalk) objects are thought of as sending and receiving 'messages'.

In Ruby, Object, the base class of everything, has a send method: Object.send For example:

class Klass
def hello
"Hello!"
end
end
k = Klass.new
k.send :hello #=> "Hello!"
k.hello #=> "Hello!"

In both of these cases k is the receiver of the 'hello' message.

How to display sender and receiver avatars in Mailboxer?

The problem was showing the conversation originator's image in the messages partial by using this method conversation.originator.profile.avatar.url.

So the fix is to use the association between messages, user and profile to get the image of the sender.

message.sender.profile.avatar.url

k.send :hello - if k is the receiver, who is the sender?

Whatever object contains this code is sending the message — presumably main. Let's look at it with more explicit objects and normal message-passing.

class Person
attr_accessor :first_name, :last_name
def initialize(first_name, last_name)
@first_name, @last_name = first_name, last_name
end
def marry(other)
self.last_name = other.last_name
end
end

bob = Person.new('Bob', 'Smith')
patty = Person.new('Patricia', 'Johnson')

patty.marry bob

In the last line of this code, main is sending marry to patty, and patty in turn sends herself last_name= and sends bob last_name.

Ruby on Rails Params set recipient and sender with users_id

Try changing your form to this:

<%= form_for @message do |f| %>
<%= f.hidden_field :sender_id, value: current_user.id %>
<%= f.text_field :title %>
<%= f.text_field :body %>
<%= f.submit %>
<% end %>

Displaying Sender and Receiver user profile on each micropost

To eliminate some confusion and make it a bit more railsy, I'd go with:

class Micropost < ActiveRecord::Base
belongs_to :sending_user, :class_name=>"User", :foreign_key=>"user_id"
belongs_to :receiving_user, :class_name=>"User", :foreign_key=>"belongs_to_id"
end

this will allow something like this in your view for a given Micropost object "@micropost":

 <%= link_to(@micropost.sending_user.username, user_path(@micropost.sending_user)) %>

<%= link_to(@micropost.receiving_user.username, user_path(@micropost.receiving_user)) %>

*this assumes several things about the user object and routing, but should get you on the right path.

Is the caller in Java the same as the receiver in Ruby?

In your example x is not calling hello(). Whatever object contains that snippet is "calling" (i.e., it's the "caller"). In Java, x can be referred to as the receiver; it is receiving the call to the hello() method.



Related Topics



Leave a reply



Submit