Ruby Conditional-Assignment and Private Methods

Ruby Conditional-Assignment and Private Methods

That looks like a bug.

UPDATE: The bug was fixed in trunk, and is slated for back porting to 2.1 and 2.0.

Note that the problem is more general than that, it is broken for all abbreviated assignments, not just conditional abbreviated assignments:

private def foo=(*) end
public def foo; 0 end

self.foo = 42

self.foo += 42
# private method `foo=' called for main:Object (NoMethodError)

private :foo

self.foo += 42
# private method `foo' called for main:Object (NoMethodError)

Ruby conditional assignment

So you ask for the upper limit:

UPPER_LIMIT = 25
b = 100
a = [b, UPPER_LIMIT].min

a will always smaller or equal to 25.

Conditional assignment from a function with multiple return values in Ruby?

Ruby doesn't support multiple return values. It's simply syntax sugar for returning a list. Hence, the following works.

@bar ||= foo[0]

Is assignment in a conditional clause good ruby style?

It is GOOD style to use assignments in conditionals. If you do so, wrap the condition in parentheses.

# bad (+ a warning)
if v = array.grep(/foo/)
do_something(v)
# some code
end

# good (MRI would still complain, but RuboCop won't)
if (v = array.grep(/foo/))
do_something(v)
# some code
end

# good
v = array.grep(/foo/)
if v
do_something(v)
# some code
end

See the community style guide for more information

Ruby: Use the return of the conditional for variable assignment and comparison

What the warning is telling you to do is:

res = if block_given?
yield(array[i], array[i+1])
else
array[i] - array[i+1]
end

That is, having a single assignment instead of two (or even more).

Style/ConditionalAssignment: Use the return of the conditional for variable assignment and comparison

Instead of

if inactive_list.include? <<id of data>>
data[:active] = false
else
data[:active] = true
end

you can just write

data[:active] = !inactive_list.include? <<id of data>>

Or when you are using Rails then you can use exclude? instead of the negation of include?:

data[:active] = inactive_list.exclude? <<id of data>>

Full example:

data = {
name: "Test",
full_name: "Test data",
active: inactive_list.exclude?(<<id of data>>)
}

Conditional assignment in Ruby

It basically leans on Ruby's falsy objects: nil and false.

Everything except for nil and false is said to be truthy in Ruby.

So in the example it prints the value of @two if it's truthy (2), otherwise (nil) it prints the error message.

I do not think this "concept" has a name.

Why is this Ruby conditional assignment not memoized?

The issue is this line:

UserPresenter.should_receive(:new).once

This chunk of code simply sets up the expectation that a UserPresenter will receive the new method one time. It tears down your original UserPresenter#new method and replaces it with a method that returns nil. Because nil is falsy, the @presenter instance variable is not memoized.

To fix this, you can specify a return value in your expectation:

UserPresenter.should_receive(:new).once.and_return "some truthy value"

or equivalently

UserPresenter.should_receive(:new).once { "some truthy value" }

or if you absolutely want to call the original method

UserPresenter.should_receive(:new).once.and_call_original

or with the new expect syntax

expect(UserPresenter).to receive(:new).once.and_call_original

See this for more information about expecting a message, and this for more information about calling the original method. This has some further discussion about RSpec's should_receive method.

Conditional assignment with empty strings

This is where you use present?'s brother, presence (assuming you use rails or at least active support).

x = this_var.presence || that_var.presence || last_resort


Related Topics



Leave a reply



Submit