Subscribe: Comments for Ruby Inside
http://www.rubyinside.com/comments/feed/
Added By: Feedage Forager Feedage Grade B rated
Language: English
Tags:
code thread  comment gil  condition valued  condition  gil make  make ruby  ruby code  ruby  sheep  str  thread safe  thread 
Rate this Feed
Rate this feedRate this feedRate this feedRate this feedRate this feed
Rate this feed 1 starRate this feed 2 starRate this feed 3 starRate this feed 4 starRate this feed 5 star

Comments (0)

Feed Details and Statistics Feed Statistics
Preview: Comments for Ruby Inside

Comments for Ruby Inside





Last Build Date: Thu, 16 Oct 2014 07:22:42 +0000

 



Comment on Ruby’s Unary Operators and How to Redefine Their Functionality by zipizap

Thu, 16 Oct 2014 07:22:42 +0000

It seems dangerous to override the "!" operator, because when used in conditions (if, until, ...) the condition may not evaluate as expected... For example (in Ruby 1.9):
>> str = "hey"
"hey"
>> puts "if condition valued to true" if (str)      #(str) condition evaluated to "true"
if condition valued to true
nil
>> puts "if condition valued to true" if (!str)     #(str) condition evaluated to "false"
nil

### untill here, everything was normall
### now we overload the ! operator of "str" , and see how it impacts the if conditions-evaluation:

>> def str.!; swapcase; end
nil

>> puts "if condition valued to true" if (str)        #(str) condition evaluated to "true", as before
if condition valued to true
nil
>> puts "if condition valued to true" if (!str)       #(str) condition evaluated to **"true"** ----> because of the ! overloading
if condition valued to true
nil
It seems a very practical option to overload unary operators, but I should not use it for the "!", as it may give unexpected results inside conditions Great post :)



Comment on Does the GIL Make Your Ruby Code Thread-Safe? by Jesse Storimer

Tue, 25 Jun 2013 14:32:04 +0000

Brian, Alex: I understand what you're saying now. You're exactly right. The data structure ensures that only one thread will shear the sheep, so the conditional is unnecessary. I updated the code example accordingly.



Comment on Does the GIL Make Your Ruby Code Thread-Safe? by Brian

Fri, 21 Jun 2013 18:34:09 +0000

I think what Alex is trying to say is that checking sheep.shorn? is not needed, since sheep_queue.pop(true) will raise ThreadError if the queue is empty. The Queue has eliminated the chance of the sheep being sheared twice. So the example could be: Thread.new do begin sheep_queue.pop(true).shear! rescue ThreadError end end Or even: Thread.new do sheep = sheep_queue.pop(true) rescue nil sheep.shear! if sheep end Unless Sheep#shear! could raise BrokenShearsError :) Ruby Inside: These comments really need a preview button. (hope I got it right) Jessie: I've really enjoyed your work. Keep it up!



Comment on Does the GIL Make Your Ruby Code Thread-Safe? by Jesse Storimer

Fri, 21 Jun 2013 11:50:48 +0000

Alex: not sure what you're referring to, can you give more context? Soleone: Thanks! More coming soon :)



Comment on Does the GIL Make Your Ruby Code Thread-Safe? by Alex

Fri, 21 Jun 2013 06:05:33 +0000

Hello! Like this article about GIL. What you mean by "this would work without GIL on Java"? What I need to do?



Comment on Does the GIL Make Your Ruby Code Thread-Safe? by Alexandr

Thu, 20 Jun 2013 15:19:28 +0000

Hello, Jesse! 5.times.map do Thread.new do begin new_sheep = sheep_queue.pop(true) unless sheep.shorn? sheep.shear! end rescue ThreadError # raised by Queue#pop in the threads # that don't pop the sheep end end end.each(&:join) is not a rewrite of a Mutex version unless sheep.shorn? which would look like mutex = Mutex.new 5.times.map do Thread.new do mutex.synchronize do unless sheep.shorn? sheep.shear! end end end end.each(&:join) , but a version without any sheep.shorn? checking like that mutex = Mutex.new 5.times.map do Thread.new do mutex.synchronize do begin sheep.shear! sheep = nil rescue end end end end.each(&:join)



Comment on Does the GIL Make Your Ruby Code Thread-Safe? by Soleone

Thu, 20 Jun 2013 14:18:38 +0000

Anazing read, thanks for putting that together! Looking forward to possibly more guest posts? :)



Comment on Does the GIL Make Your Ruby Code Thread-Safe? by Bob

Wed, 19 Jun 2013 15:42:43 +0000

Nice article. In your reflections, I think your quip should have said: "5 out of 4 dentists agree that multi-threaded programming is hard to get right."






Comment on The Split is Not Enough: Unicode Whitespace Shenigans for Rubyists by Jonas Elfström

Wed, 12 Dec 2012 09:44:19 +0000

If I'm not mistaken it's not so much that C# supports these shenanigans but rather that Bogard used a font that rendered a non-visible character as a space.