what is difference between -> and => in laravel - laravel-5

This is the code where we have used -> and => .
But I always get confused, while writing code at that time which one to use and where.
So seeking its logic to easily remember it.
$quan= $request->all();
ChaiExl::create(['date'=>date('Y-m-d',strtotime($quan['dt'])),'quantity'=>$quan['quan']]);
return view('edit',['row'=>$row]);

-> and => are both operators.
The difference is that => is the assign operator that is used while creating an array.
For example:
array(key => value, key2 => value2)
And
-> is the access operator. It accesses an object's value

This is PHP syntax, not laravel specifics.
=> is for setting values in arrays:
$foobar = array(
'bar' => 'something',
'foo' => 222
);
or
$foobar = [
'bar' => 'something',
'foo' => 222
];
-> is used for calling class methods and properties:
class MyClass {
public $bar = 'something';
public function foo() {
}
}
$foobar = new MyClass();
$foobar->foo();
echo $foobar->bar;

=> is used in associative array key value assignment. look below:
array(
key => value,
key2 => value2,
key3 => value3,
...
)
-> is used to access an object method or property. Example:
$object->method() or $object->var1

When you want to access a method from a class you will use -> that is
$class = new Class;
$class->mymethod();
but when you want to declare an array of object pairs you use => that is
$property = ('firstproperty', ['second'=>'secondPair','third'=>'thirdPair'])

Related

Elegantly return value(s) matching criteria from an array of nested hashes - in one line

I have been searching for a solution to this issue for a couple of days now, and I'm hoping someone can help out. Given this data structure:
'foo' => {
'bar' => [
{
'baz' => {'faz' => '1.2.3'},
'name' => 'name1'
},
{
'baz' => {'faz' => '4.5.6'},
'name' => 'name2'
},
{
'baz' => {'faz' => '7.8.9'},
'name' => 'name3'
}
]
}
I need to find the value of 'faz' that begins with a '4.', without using each. I have to use the '4.' value as a key for a hash I will create while looping over 'bar' (which obviously I can't do if I don't yet know the value of '4.'), and I don't want to loop twice.
Ideally, there would be an elegant one-line solution to return the value '4.5.6' to me.
I found this article, but it doesn't address the full complexity of this data structure, and the only answer given for it is too verbose; the looping-twice solution is more readable. I'm using Ruby 2.3 on Rails 4 and don't have the ability to upgrade. Are there any Ruby gurus out there who can guide me?
You can use select to filter results.
data = {'foo' => {'bar' => [{'baz' => {'faz' => '1.2.3'}, 'name' => 'name1'}, {'baz' => {'faz' => '4.5.6'}, 'name' => 'name2'}, {'baz' => {'faz' => '7.8.9'}, 'name' => 'name3'}]}}
data.dig('foo', 'bar').select { |obj| obj.dig('baz', 'faz').slice(0) == '4' }
#=> [{"baz"=>{"faz"=>"4.5.6"}, "name"=>"name2"}]
# or if you prefer the square bracket style
data['foo']['bar'].select { |obj| obj['baz']['faz'][0] == '4' }
The answer assumes that every element inside the bar array has the nested attributes baz -> faz.
If you only expect one result you can use find instead.

Rendering a hash in a Rails View

I have a hash like this
valuehash={:a => { "test1"=>"testing1"} , :b => {"test2" => "testing2"}}
I want my response as below
value : [ { "test1" :"testing1"} ,
{"test2" : "testing2"}]
I have tried below code in my View but it doesn't seem to work
json.value do
json.child! do
json.template! valuehash[:a]
end
json.child! do
json.template! valuehash[:b]
end
end
Can any one tell me what is the problem with my code .Also to need I need to do some modification to value hash based on some conditions so I don't want to use map,select but want to use something like above to render.
valuehash={:a => { "test1"=>"testing1"} , :b => {"test2" => "testing2"}}
# {:a=>{"test1"=>"testing1"}, :b=>{"test2"=>"testing2"}}
value = valuehash.map { |_,val| val }
# [{"test1"=>"testing1"}, {"test2"=>"testing2"}]

Dynamic parameters using variable as key

Lets say I have a method foo:
def foo(options = {})
...
end
I want to pass a variable as the key.
fooKey = "option1"
foo(fooKey: "bar1", option2: 'bar2')
Is this possible?
Just use the hashrocket syntax
foo(fooKey => 'bar1', :option2 => 'bar2')

ruby hash add key/value if modifier

I am working with a ruby hash that contains key/value pairs for creating a new subscriber in my MailChimp API.
user_information = {
'fname' => 'hello world',
'mmerge1' => 'Product X' if user.product_name.present?
}
Obviously, I am getting a syntax error of syntax error, unexpected modifier_if... I am basically only wanting to add the mmerge1 based upon the conditional being true.
You can't use if that way, inside a hash-initialization block. You'll have to conditionally add the new key/value after initializing the hash:
user_information = {
'fname' => 'hello world',
}
user_information['mmerge1'] = 'Product X' if user.product_name.present?
user_information = {'fname' => 'hello world'}
user_information.merge!({'mmerge1' => 'Product X'}) if user.product_name.present?
#=> {"fname"=>"hello world", "mmerge1"=>"Product X"}
If mmerge1 is allowed to be nil or an empty string, you can use the ?: ternary operator inside the hash:
user_information = {
'fname' => 'hello world',
'mmerge1' => user.product_name.present? ? 'Product X' : ''
}
If you use a conditional expression on the key you get a reasonably readable syntax and, at most, need to remove only 1 element from the Hash.
product_name = false
extra_name = false
user_information = {
'fname' => 'hello world',
product_name ? :mmerge1 : nil => 'Product X',
extra_name ? :xmerge1 : nil => 'Extra X'
}
user_information.delete nil
p user_information

Anonymous method inside hash assignment

Is there anything like this possible in Ruby:
hash = { :foo => 'bar', :bar => lambda{ condition ? return 'value1' : return 'value2'}}
That actual code doesn't work (clearly), and I know I could just do the logic before the hash assignment, but it would be nice to work inside the assignment like this. Is such a thing possible?
You don't need a lambda for that, just this should work:
hash = {
:foo => 'bar',
:bar => condition ? 'value1' : 'value2'
}
Or if you want to use a function result on hash,
hash= {
:foo=> 'foooooo',
:bar=> lambda {
if condition
value1
else
value2
end
}.call
}

Resources