Seeing double: how Ruby shares string values. How many times do you think Ruby allocates memory for the “Lorem ipsum…” string while running this code snippet?
Str = "Lorem ipsum dolor sit amet, consectetur adipisicing elit" str2 = str …or what about while running this snippet? Str = "Lorem ipsum dolor sit amet, consectetur adipisicing elit" str2 = String.new(str) …and this one? Str = "Lorem ipsum dolor sit amet, consectetur adipisicing elit" str2 = str.dup str2.upcase! Or this one? Str = "Lorem ipsum dolor sit amet, consectetur adipisicing elit" str2 = str[1..-1] The answers are not what you expect! Referring to one String object with two variables Two weeks ago I used this example to illustrate how Ruby shares string values: Here’s a diagram showing how this string value is shared by str and str2: As Evan Phoenix pointed out in a comment on my last post, I was actually incorrect to use this as an example of a shared string.
Running this code I get the following output: Sharing one string value between two String objects Running this: A Ruby HOWTO: Writing A Method That Uses Code Blocks. A Ruby HOWTO: Writing A Method That Uses Code Blocks One of the first things about Ruby which absolutely delighted me was its implementation of code blocks.
They take a potentially ugly construct–anonymous delegates–and turn them into a readable, time-saving structure: chunky_bacon = %w(moo hoo ha ha) chunky_bacon.each { |cb| puts "--> #{cb}" } So how can we use this wonderful little bit of functionality in our own code? Well, break out a text editor and a terminal window, because it’s time to write some Ruby! Wait… why are code blocks cool? Okay, maybe there’re a few skeptics out there who are thinking to themselves “Gosh, Coda… this all sounds super-swell, but I’m not just not convinced that the classes I write could use code blocks.” Transactions: Do you have something which needs to be done atomically?
Got it? Yay! If you’re anything like me, you immediately jumped to the RDoc info for the Ruby Core, and took a gander at the listing for Array#each: array.each {|item| block } → array. Algorithme déterministe de génération de couleurs. Dans un programme il arrive parfois que vous ayez à générer une suite de nombres aléatoire.
Pour cela rand fait très bien l’affaire. Par contre vous pouvez avoir besoin de générer une suite de nombres déterministes, c’est à dire que la suite générée sera toujours la même. Dans mon cas il s’agit de générer un tableau de couleurs pour un chart. Le nombre de couleurs n’est pas défini à l’avance mais je souhaite que les couleurs générées soit toujours les même pour éviter que le graphique change complètement à chaque rafraichissement.
Lorsqu’on utilise un générateur comme rand il commence par initialiser son seed, un nombre sur lequel sera basé la génération des nombres. Ce nombre est normalement initialisé de façon complètement aléatoire, en se basant sur /dev/random ou /dev/urandom (sur les systèmes UNIX) pour maximiser l’entropie des nombres générés. Le but étant que les nombres générés soient le moins «devinable» possible. Pour cela en ruby 1.8 on peut utiliser srand: L’équipe Synbioz.