tag:blogger.com,1999:blog-9473353925502817312024-02-08T13:08:40.817+03:00Walk and ye shall reach.Anonymoushttp://www.blogger.com/profile/01888458412282498727noreply@blogger.comBlogger5125tag:blogger.com,1999:blog-947335392550281731.post-12764306246236102512011-07-28T21:43:00.000+03:002016-03-05T21:23:15.710+03:00#changeDo you now about change?<br />
In Rails 3.1 ActiveRecord::Migration we can define only one method<br />
change<br />
It`s 'reversible' migrations. You just implement 'up' version and Migration system generate for you methods to create revers migration.<br />
For example:<br />
<!-- HTML generated using hilite.me --><div style="background: #272822; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%"><span style="color: #66d9ef">class</span> <span style="color: #a6e22e">CreateWagons</span> <span style="color: #f92672"><</span> <span style="color: #e6db74">ActiveRecord</span><span style="color: #f8f8f2">:</span><span style="color: #e6db74">:Migration</span>
<span style="color: #66d9ef">def</span> <span style="color: #a6e22e">change</span>
<span style="color: #f8f8f2">create_table</span> <span style="color: #e6db74">:wagons</span> <span style="color: #66d9ef">do</span> <span style="color: #f92672">|</span><span style="color: #f8f8f2">t</span><span style="color: #f92672">|</span>
<span style="color: #f8f8f2">t</span><span style="color: #f92672">.</span><span style="color: #f8f8f2">integer</span> <span style="color: #e6db74">:number</span>
<span style="color: #f8f8f2">t</span><span style="color: #f92672">.</span><span style="color: #f8f8f2">integer</span> <span style="color: #e6db74">:mileage</span>
<span style="color: #f8f8f2">t</span><span style="color: #f92672">.</span><span style="color: #f8f8f2">datetime</span> <span style="color: #e6db74">:build_date</span>
<span style="color: #66d9ef">end</span>
<span style="color: #66d9ef">end</span>
<span style="color: #66d9ef">end</span>
</pre></div>
When you run migration 'up' it create a new table, if 'down' - drop dable. But you must be careful using this magick, because some methods can`t be resolved and you get ActiveRecord::IrreversibleMigration<br />
Rails known how to revert this commands:<br />
<!-- HTML generated using hilite.me --><div style="background: #272822; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%"><span style="color: #f8f8f2">add_column</span>
<span style="color: #f8f8f2">add_index</span>
<span style="color: #f8f8f2">add_timestamp</span>
<span style="color: #f8f8f2">create_table</span>
<span style="color: #f8f8f2">remove_timestamps</span>
<span style="color: #f8f8f2">rename_column</span>
<span style="color: #f8f8f2">rename_index</span>
<span style="color: #f8f8f2">rename_table</span>
</pre></div>
There for you can create 'change' migration. Try to run it 'up' and 'down', and if you migration raised, you just implement 'down' version.<br />
I think it`s one more beautiful issues that presents to us Rails platform.Anonymoushttp://www.blogger.com/profile/01888458412282498727noreply@blogger.com0tag:blogger.com,1999:blog-947335392550281731.post-5449828625073543812011-04-29T14:50:00.001+03:002016-03-05T21:27:02.763+03:00sticker<p>This sticker i must put on my display :) command to create TAGS file</p>
<!-- HTML generated using hilite.me --><div style="background: #272822; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%">$ <span style="color: #f8f8f2">cd </span>rails_root_path
$ ctags -e -a --Ruby-kinds<span style="color: #f92672">=</span>-f -o TAGS -R .
</pre></div>
Anonymoushttp://www.blogger.com/profile/01888458412282498727noreply@blogger.com1tag:blogger.com,1999:blog-947335392550281731.post-47457888477199269322011-04-22T17:56:00.000+03:002016-03-05T21:28:44.209+03:00Inside rails.<p>You must be very careful when create "before_*" callbacks in Rails.</p><p>I found one interesting feature (of course it is not bug :D ) ActiveRecord raise with error RecordNotSaved if you have "before_*" callback that return "false". For example:</p>
<!-- HTML generated using hilite.me --><div style="background: #272822; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%"><span style="color: #66d9ef">class</span> <span style="color: #a6e22e">User</span> <span style="color: #f92672"><</span> <span style="color: #e6db74">ActiveRecord</span><span style="color: #f8f8f2">:</span><span style="color: #e6db74">:Base</span>
<span style="color: #f8f8f2">before_save</span> <span style="color: #e6db74">:set_short_link</span>
<span style="color: #66d9ef">private</span>
<span style="color: #66d9ef">def</span> <span style="color: #a6e22e">set_short_link</span>
<span style="color: #f8f8f2">short_link</span> <span style="color: #f92672">=</span> <span style="color: #f8f8f2">name</span><span style="color: #f92672">.</span><span style="color: #f8f8f2">to_url</span> <span style="color: #66d9ef">if</span> <span style="color: #f8f8f2">name</span><span style="color: #f92672">.</span><span style="color: #f8f8f2">changed?</span>
<span style="color: #66d9ef">end</span>
<span style="color: #66d9ef">end</span>
</pre></div>
<p>You can`t save instance of this class if you don`t change "name" because method "set_short_link" return false (see /activerecord/lib/active_record/base.rb, line 2568)</p><p>Right version will be:</p><!-- HTML generated using hilite.me --><div style="background: #272822; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%"><span style="color: #66d9ef">class</span> <span style="color: #a6e22e">User</span> <span style="color: #f92672"><</span> <span style="color: #e6db74">ActiveRecord</span><span style="color: #f8f8f2">:</span><span style="color: #e6db74">:Base</span>
<span style="color: #f8f8f2">before_save</span> <span style="color: #e6db74">:set_short_link</span>
<span style="color: #66d9ef">private</span>
<span style="color: #66d9ef">def</span> <span style="color: #a6e22e">set_short_link</span>
<span style="color: #f8f8f2">short_link</span> <span style="color: #f92672">=</span> <span style="color: #f8f8f2">name</span><span style="color: #f92672">.</span><span style="color: #f8f8f2">to_url</span> <span style="color: #66d9ef">if</span> <span style="color: #f8f8f2">name</span><span style="color: #f92672">.</span><span style="color: #f8f8f2">changed?</span>
<span style="color: #66d9ef">true</span>
<span style="color: #66d9ef">end</span>
<span style="color: #66d9ef">end</span>
</pre></div>
Anonymoushttp://www.blogger.com/profile/01888458412282498727noreply@blogger.com3tag:blogger.com,1999:blog-947335392550281731.post-5497487554103533852010-11-19T19:12:00.004+02:002016-03-05T21:45:12.552+03:00All about ~/.emacs<p>As most of the time I work with Ruby and Ruby on Rails and Emacs is my favorite text editor I would like to devote my next post to the Emacs configuration.</p><p>Last week I have been customizing Emacs in order to simplify my work. Therefore, i would like to share my work with you.</p><p><b>Theme</b></p><p>I started with Emacs theme customization and used color-theme plugin, which can be found on <a href="http://www.nongnu.org/color-theme/">Emacs color-theme plugin homepage</a> or can be taken from Debian repository in emacs-goodies-el package. After the installation you have three options:</p><ol><li>Use one of the build-in themes (M-x color-theme-select);</li>
<li>Create your own theme;</li>
<li>Use mine :) (<a href="https://gist.github.com/664177">color theme</a>)</li>
</ol><p>For the options 2 and 3 you have to save the theme file into Emacs path directory and add the following line into your ~/.emacs file:</p>
<!-- HTML generated using hilite.me --><div style="background: #272822; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%"><span style="color: #75715e">;; Theme</span>
<span style="color: #f8f8f2">(load-file</span> <span style="color: #e6db74">"~/.emacs.d/color-theme-butcher.el"</span><span style="color: #f8f8f2">)</span>
<span style="color: #f8f8f2">(color-theme-butcher)</span>
<span style="color: #f8f8f2">(set-frame-font</span> <span style="color: #e6db74">"-misc-fixed-medium-r-normal--15-*-75-75-c-90-koi8-r"</span><span style="color: #f8f8f2">)</span>
<span style="color: #f8f8f2">(tool-bar-mode)</span>
<span style="color: #f8f8f2">(menu-bar-mode)</span>
<span style="color: #f8f8f2">(scroll-bar-mode)</span>
</pre></div>
<p>Also, as you have noticed, I set the custom font using set-frame-font (the font was chosen in xfontsel), disabled: toolbar, menubar and scrollbar.</p><p><b>Rinari</b></p><p>Afterwards, I chose Rinari from a large list of Emacs Rails plugins. <a href="http://rinari.rubyforge.org/index.html">Rinari homepage.</a></p><p>Rinari installation:</p>
<!-- HTML generated using hilite.me --><div style="background: #272822; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%">$ git clone git://github.com/eschulte/rinari.git
$ <span style="color: #f8f8f2">cd </span>rinari
$ git submodule init
$ git submodule update
</pre></div>
<p>The base Rinari directory is placed into Emacs path directory and the following code is added to ~/.emacs </p>
<!-- HTML generated using hilite.me --><div style="background: #272822; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%"><span style="color: #75715e">;; Rinari</span>
<span style="color: #f8f8f2">(add-to-list</span> <span style="color: #e6db74">'load-path</span> <span style="color: #e6db74">"~/.emacs.d/rinari/"</span><span style="color: #f8f8f2">)</span>
<span style="color: #f8f8f2">(require</span> <span style="color: #e6db74">'rinari</span><span style="color: #f8f8f2">)</span>
<span style="color: #f8f8f2">(add-hook</span> <span style="color: #e6db74">'ruby-mode-hook</span>
<span style="color: #f8f8f2">(</span><span style="color: #66d9ef">lambda</span> <span style="color: #f8f8f2">()</span>
<span style="color: #f8f8f2">(defadvice</span> <span style="color: #f8f8f2">ruby-mode-set-encoding</span>
<span style="color: #f8f8f2">(around</span> <span style="color: #f8f8f2">ruby-mode-set-encoding-disable</span> <span style="color: #f8f8f2">activate)</span> <span style="color: #66d9ef">nil</span><span style="color: #f8f8f2">)))</span>
</pre></div>
<p>The last hook for ruby-mode deactivates Emacs coding tags, automatically created by Emacs coding-system (that can be very uncomfortable while working with files that have Cyrillic symbols).</p><p><b>Ruby Electric</b></p><p>Unfortunately, Rinari does not include ruby-electric (helper for automatic brackets closure or any other language construction). To add ruby-electric copy ruby-electric.el to Emacs path directory and configure it into ~/.emacs</p>
<!-- HTML generated using hilite.me --><div style="background: #272822; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%"><span style="color: #75715e">;; Ruby-electric</span>
<span style="color: #f8f8f2">(require</span> <span style="color: #e6db74">'ruby-electric</span><span style="color: #f8f8f2">)</span>
<span style="color: #f8f8f2">(add-hook</span> <span style="color: #e6db74">'ruby-mode-hook</span> <span style="color: #e6db74">'ruby-electric-mode</span><span style="color: #f8f8f2">)</span>
</pre></div>
<p><b>MultiMajorModes</b></p><p>Generally, Emacs uses only one major editing mode, for example, ruby-mode in *.rb files. However, if we want to edit files that contain ruby and HTML we will have to either switch the modes every time or we can install MuMaMo (MultiMajorModes). MuMaMo automatically highlights the code and switches the major modes. Most often MuMaMo is used in *.html.erb files to edit ruby and HTML. MuMaMo is based on nXhtml. You can download the latest version of nXhtml (MuMaMo included) <a href="http://ourcomments.org/cgi-bin/emacsw32-dl-latest.pl">here</a>.</p>Unzip the archive and copy it into Emacs path directory. Add to ~/.emacs the following:</p>
<!-- HTML generated using hilite.me --><div style="background: #272822; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%"><span style="color: #75715e">;; MuMaMo-Mode for rhtml files</span>
<span style="color: #f8f8f2">(add-to-list</span> <span style="color: #e6db74">'load-path</span> <span style="color: #e6db74">"~/.emacs.d/nxhtml/util/"</span><span style="color: #f8f8f2">)</span>
<span style="color: #f8f8f2">(require</span> <span style="color: #e6db74">'mumamo-fun</span><span style="color: #f8f8f2">)</span>
<span style="color: #f8f8f2">(</span><span style="color: #66d9ef">setq</span> <span style="color: #f8f8f2">mumamo-chunk-coloring</span> <span style="color: #ae81ff">5</span><span style="color: #f8f8f2">)</span>
<span style="color: #f8f8f2">(add-to-list</span> <span style="color: #e6db74">'auto-mode-alist</span> <span style="color: #f92672">'</span><span style="color: #f8f8f2">(</span><span style="color: #e6db74">"\\.rhtml\\'"</span> <span style="color: #f92672">.</span> <span style="color: #f8f8f2">eruby-html-mumamo))</span>
<span style="color: #f8f8f2">(add-to-list</span> <span style="color: #e6db74">'auto-mode-alist</span> <span style="color: #f92672">'</span><span style="color: #f8f8f2">(</span><span style="color: #e6db74">"\\.html\\.erb\\'"</span> <span style="color: #f92672">.</span> <span style="color: #f8f8f2">eruby-html-mumamo))</span>
</pre></div>
<p>As I don`t like colored hunks, I set the variable MuMaMo-chunk-coloring being equal 5. This means that the coloring will turn on only for the fifth level, however, usually we see only three levels maximum.</p><p><b>Interactively Do Things</b></p><p>To speed up file management in you working directory you can use IDO helper (Interactively Do Things), which is a part of Rinari package. If you dislike it you can disable it any time.</p>
<!-- HTML generated using hilite.me --><div style="background: #272822; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%"><span style="color: #75715e">;; Interactively Do Things</span>
<span style="color: #f8f8f2">(require</span> <span style="color: #e6db74">'ido</span><span style="color: #f8f8f2">)</span>
<span style="color: #f8f8f2">(ido-mode</span> <span style="color: #66d9ef">t</span><span style="color: #f8f8f2">)</span>
</pre></div>
<p><b>Git in Emacs</b></p><p>As I use git a lot I included git-emacs (<a href="https://github.com/tsgates/git-emacs">link</a>) in Emacs as well:</p>
<!-- HTML generated using hilite.me --><div style="background: #272822; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%"><span style="color: #75715e">;; git-emacs</span>
<span style="color: #f8f8f2">(add-to-list</span> <span style="color: #e6db74">'load-path</span> <span style="color: #e6db74">"~/.emacs.d/git-emacs/"</span><span style="color: #f8f8f2">)</span>
<span style="color: #f8f8f2">(require</span> <span style="color: #e6db74">'git-emacs</span><span style="color: #f8f8f2">)</span>
<span style="color: #f8f8f2">(require</span> <span style="color: #e6db74">'git-status</span><span style="color: #f8f8f2">)</span>
<span style="color: #f8f8f2">(</span><span style="color: #66d9ef">setq</span> <span style="color: #f8f8f2">git-state-modeline-decoration</span> <span style="color: #e6db74">'git-state-decoration-large-dot</span><span style="color: #f8f8f2">)</span>
<span style="color: #f8f8f2">(global-set-key</span> <span style="color: #f8f8f2">(kbd</span> <span style="color: #e6db74">"C-x M-g"</span><span style="color: #f8f8f2">)</span> <span style="color: #e6db74">'git-status</span><span style="color: #f8f8f2">)</span>
</pre></div>
<p>In the last line I bound git-status function to C-x M-g combination.</p><p><b>Autocompletion.</b></p><p>Autocomplete plugin can be downloaded <a href="http://cx4a.org/software/auto-complete/">here</a>. Unpack the plugin, copy to Emacs path and configure it:</p>
<!-- HTML generated using hilite.me --><div style="background: #272822; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%"><span style="color: #75715e">;; Autocomplete</span>
<span style="color: #f8f8f2">(add-to-list</span> <span style="color: #e6db74">'load-path</span> <span style="color: #e6db74">"~/.emacs.d/"</span><span style="color: #f8f8f2">)</span>
<span style="color: #f8f8f2">(require</span> <span style="color: #e6db74">'auto-complete-config</span><span style="color: #f8f8f2">)</span>
<span style="color: #f8f8f2">(add-to-list</span> <span style="color: #e6db74">'ac-dictionary-directories</span> <span style="color: #e6db74">"/home/butcher/.emacs.d//ac-dict"</span><span style="color: #f8f8f2">)</span>
<span style="color: #f8f8f2">(ac-config-default)</span>
</pre></div>
<p><b>Rsense</b></p><p>As autocomplete has a short completion list. I used <a href="http://cx4a.org/software/rsense/">Rsense</a> to extend autocomplete functionality. Further on I will tell how to install and configure Rsense in Debian (more detailed information for other OS you can find in Rsense installation guide).</p><p>So, having downloaded and unpacked the archive copy it into /opt directory and add local variable RSENSE_HOME that will keep the path to Rsense root directory. Afterwards, check whether bin/rsense can be executed and then create config file from Rsense configuration script.</p>
<!-- HTML generated using hilite.me --><div style="background: #272822; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%">$ wget http://cx4a.org/pub/rsense/rsense-0.3.zip
$ unzip rsence-0.3
$ cp -av rsense-0.3 /opt
</pre></div>
<p>What comes to the local variable RSENSE_HOME, add to ~/.bashrc the following line using your favorite editor:</p>
<!-- HTML generated using hilite.me --><div style="background: #272822; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%">export RSENSE_HOME=/opt/rsense-0.3/
</pre></div>
<p>Save the file, close the editor and open new console tab to re-read the configuration file.</p>
<!-- HTML generated using hilite.me --><div style="background: #272822; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%">$ <span style="color: #f8f8f2">echo</span> <span style="color: #f8f8f2">$RSENSE_HOME</span>
/opt/rsense-0.3/
$ <span style="color: #f8f8f2">cd</span> <span style="color: #f8f8f2">$RSENSE_HOME</span>
$ chmod +x bin/rsense
$ bin/rsense version
RSense 0.3
$ ruby etc/config.rb > ~/.rsense
$ cat ~/.rsense
home = /opt/rsense-0.3
load-path = /usr/local/lib/site_ruby/1.8:/usr/local/lib/site_ruby/1.8/i486-linux:/usr/local/lib/site_ruby/1.8/i386-linux:/usr/local/lib/site_ruby:/usr/lib/ruby/vendor_ruby/1.8:/usr/lib/ruby/vendor_ruby/1.8/i486-linux:/usr/lib/ruby/vendor_ruby:/usr/lib/ruby/1.8:/usr/lib/ruby/1.8/i486-linux:/usr/lib/ruby/1.8/i386-linux:.
gem-path = /home/butcher/.gem/ruby/1.8:/var/lib/gems/1.8
</pre></div>
<p>That is all about Rsense installation.</p><p><b>Autocomplete with Rsense</b></p><p>To configure Emacs for Rsense and automplete + Rsense work:</p>
<!-- HTML generated using hilite.me --><div style="background: #272822; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%"><span style="color: #75715e">;; Rsense</span>
<span style="color: #f8f8f2">(</span><span style="color: #66d9ef">setq</span> <span style="color: #f8f8f2">rsense-home</span> <span style="color: #e6db74">"/opt/rsense-0.3"</span><span style="color: #f8f8f2">)</span>
<span style="color: #f8f8f2">(add-to-list</span> <span style="color: #e6db74">'load-path</span> <span style="color: #f8f8f2">(concat</span> <span style="color: #f8f8f2">rsense-home</span> <span style="color: #e6db74">"/etc"</span><span style="color: #f8f8f2">))</span>
<span style="color: #f8f8f2">(require</span> <span style="color: #e6db74">'rsense</span><span style="color: #f8f8f2">)</span>
<span style="color: #75715e">;; Rsense + Autocomplete</span>
<span style="color: #f8f8f2">(add-hook</span> <span style="color: #e6db74">'ruby-mode-hook</span>
<span style="color: #f8f8f2">(</span><span style="color: #66d9ef">lambda</span> <span style="color: #f8f8f2">()</span>
<span style="color: #f8f8f2">(add-to-list</span> <span style="color: #e6db74">'ac-sources</span> <span style="color: #e6db74">'ac-source-rsense-method</span><span style="color: #f8f8f2">)</span>
<span style="color: #f8f8f2">(add-to-list</span> <span style="color: #e6db74">'ac-sources</span> <span style="color: #e6db74">'ac-source-rsense-constant</span><span style="color: #f8f8f2">)))</span>
</pre></div>
<p>That`s all folks! :) You can borrow my final version of ~/.emacs file <a href="https://gist.github.com/664172">here</a></p><p>Have a nice coding on Ruby on Rails with Emacs!</p>Anonymoushttp://www.blogger.com/profile/01888458412282498727noreply@blogger.com6tag:blogger.com,1999:blog-947335392550281731.post-23539624621731088672010-10-28T14:40:00.003+03:002016-03-05T21:32:43.743+03:00Ruby Deal-breakerRecently I have come across a very interesting thing while working with Ruby arrays. My goal was to convert the array into a SQL query. For example, converting the array [[1,2,3],[4,5,6,],[7,8,9,]] into the string "(1,2,3),(4,5,6),(7,8,9)". If one has any basic knowledge about iterators, it won’t be a difficulty at all.<br />
<br />
Example below:<br />
<!-- HTML generated using hilite.me --><div style="background: #272822; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%"><span style="color: #f8f8f2">irb(main):</span><span style="color: #ae81ff">001</span><span style="color: #f8f8f2">:</span><span style="color: #ae81ff">0</span><span style="color: #f92672">></span> <span style="color: #f8f8f2">array</span> <span style="color: #f92672">=</span> <span style="color: #f92672">[[</span><span style="color: #ae81ff">1</span><span style="color: #f8f8f2">,</span><span style="color: #ae81ff">2</span><span style="color: #f8f8f2">,</span><span style="color: #ae81ff">3</span><span style="color: #f92672">]</span><span style="color: #f8f8f2">,</span> <span style="color: #f92672">[</span><span style="color: #ae81ff">4</span><span style="color: #f8f8f2">,</span><span style="color: #ae81ff">5</span><span style="color: #f8f8f2">,</span><span style="color: #ae81ff">6</span><span style="color: #f92672">]</span><span style="color: #f8f8f2">,</span> <span style="color: #f92672">[</span><span style="color: #ae81ff">7</span><span style="color: #f8f8f2">,</span><span style="color: #ae81ff">8</span><span style="color: #f8f8f2">,</span><span style="color: #ae81ff">9</span><span style="color: #f92672">]]</span>
<span style="color: #f92672">=></span> <span style="color: #f92672">[[</span><span style="color: #ae81ff">1</span><span style="color: #f8f8f2">,</span> <span style="color: #ae81ff">2</span><span style="color: #f8f8f2">,</span> <span style="color: #ae81ff">3</span><span style="color: #f92672">]</span><span style="color: #f8f8f2">,</span> <span style="color: #f92672">[</span><span style="color: #ae81ff">4</span><span style="color: #f8f8f2">,</span> <span style="color: #ae81ff">5</span><span style="color: #f8f8f2">,</span> <span style="color: #ae81ff">6</span><span style="color: #f92672">]</span><span style="color: #f8f8f2">,</span> <span style="color: #f92672">[</span><span style="color: #ae81ff">7</span><span style="color: #f8f8f2">,</span> <span style="color: #ae81ff">8</span><span style="color: #f8f8f2">,</span> <span style="color: #ae81ff">9</span><span style="color: #f92672">]]</span>
<span style="color: #f8f8f2">irb(main):</span><span style="color: #ae81ff">002</span><span style="color: #f8f8f2">:</span><span style="color: #ae81ff">0</span><span style="color: #f92672">></span> <span style="color: #f8f8f2">sql_values</span> <span style="color: #f92672">=</span> <span style="color: #f8f8f2">array</span><span style="color: #f92672">.</span><span style="color: #f8f8f2">map{</span><span style="color: #f92672">|</span><span style="color: #f8f8f2">a</span><span style="color: #f92672">|</span> <span style="color: #e6db74">"("</span> <span style="color: #f92672">+</span> <span style="color: #f8f8f2">a</span><span style="color: #f92672">.</span><span style="color: #f8f8f2">join(</span><span style="color: #e6db74">", "</span><span style="color: #f8f8f2">)</span> <span style="color: #f92672">+</span> <span style="color: #e6db74">")"</span> <span style="color: #f8f8f2">}</span><span style="color: #f92672">.</span><span style="color: #f8f8f2">join(</span><span style="color: #e6db74">", "</span><span style="color: #f8f8f2">)</span>
<span style="color: #f92672">=></span> <span style="color: #e6db74">"(1, 2, 3), (4, 5, 6), (7, 8, 9)"</span>
</pre></div>
<br />
`map` returns new array which we join using "," string.<br />
Also, you can put the data into SQL query manually. For example, adding any random integer values to our data:<br />
<br />
<!-- HTML generated using hilite.me --><div style="background: #272822; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%"><span style="color: #f8f8f2">irb(main):</span><span style="color: #ae81ff">003</span><span style="color: #f8f8f2">:</span><span style="color: #ae81ff">0</span><span style="color: #f92672">></span> <span style="color: #f8f8f2">sql_values</span> <span style="color: #f92672">=</span> <span style="color: #f8f8f2">array</span><span style="color: #f92672">.</span><span style="color: #f8f8f2">map{</span><span style="color: #f92672">|</span><span style="color: #f8f8f2">a</span><span style="color: #f92672">|</span> <span style="color: #e6db74">"("</span> <span style="color: #f92672">+</span> <span style="color: #f8f8f2">(a</span> <span style="color: #f92672"><<</span> <span style="color: #f8f8f2">rand(</span><span style="color: #ae81ff">100</span><span style="color: #f8f8f2">))</span><span style="color: #f92672">.</span><span style="color: #f8f8f2">join(</span><span style="color: #e6db74">", "</span><span style="color: #f8f8f2">)</span> <span style="color: #f92672">+</span> <span style="color: #e6db74">")"</span> <span style="color: #f8f8f2">}</span><span style="color: #f92672">.</span><span style="color: #f8f8f2">join(</span><span style="color: #e6db74">", "</span><span style="color: #f8f8f2">)</span>
<span style="color: #f92672">=></span> <span style="color: #e6db74">"(1, 2, 3, 98), (4, 5, 6, 24), (7, 8, 9, 77)"</span>
</pre></div>
<br />
However, pay attention! Of course, `map` does return the new array, but you, probably, think that the `a << rand (100)` adds the new value to the output array only. You are wrong! The thing is that when you work with `a` inside the `map` iterator, you work with the basic array, as by default, the variables are passed by link in Ruby. Therefore, when the new data is added in the existing array, you will get the following: <br />
<br />
<!-- HTML generated using hilite.me --><div style="background: #272822; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%"><span style="color: #f8f8f2">irb(main):</span><span style="color: #ae81ff">004</span><span style="color: #f8f8f2">:</span><span style="color: #ae81ff">0</span><span style="color: #f92672">></span> <span style="color: #f8f8f2">array</span>
<span style="color: #f92672">=></span> <span style="color: #f92672">[[</span><span style="color: #ae81ff">1</span><span style="color: #f8f8f2">,</span> <span style="color: #ae81ff">2</span><span style="color: #f8f8f2">,</span> <span style="color: #ae81ff">3</span><span style="color: #f8f8f2">,</span> <span style="color: #ae81ff">98</span><span style="color: #f92672">]</span><span style="color: #f8f8f2">,</span> <span style="color: #f92672">[</span><span style="color: #ae81ff">4</span><span style="color: #f8f8f2">,</span> <span style="color: #ae81ff">5</span><span style="color: #f8f8f2">,</span> <span style="color: #ae81ff">6</span><span style="color: #f8f8f2">,</span> <span style="color: #ae81ff">24</span><span style="color: #f92672">]</span><span style="color: #f8f8f2">,</span> <span style="color: #f92672">[</span><span style="color: #ae81ff">7</span><span style="color: #f8f8f2">,</span> <span style="color: #ae81ff">8</span><span style="color: #f8f8f2">,</span> <span style="color: #ae81ff">9</span><span style="color: #f8f8f2">,</span> <span style="color: #ae81ff">77</span><span style="color: #f92672">]]</span>
</pre></div>
<br />
To create a new instance you must use the operation like ‘+’<br />
<br />
<!-- HTML generated using hilite.me --><div style="background: #272822; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%"><span style="color: #f8f8f2">irb(main):</span><span style="color: #ae81ff">005</span><span style="color: #f8f8f2">:</span><span style="color: #ae81ff">0</span><span style="color: #f92672">></span> <span style="color: #f8f8f2">sql_values</span> <span style="color: #f92672">=</span> <span style="color: #f8f8f2">array</span><span style="color: #f92672">.</span><span style="color: #f8f8f2">map{</span><span style="color: #f92672">|</span><span style="color: #f8f8f2">a</span><span style="color: #f92672">|</span> <span style="color: #e6db74">"("</span> <span style="color: #f92672">+</span> <span style="color: #f8f8f2">(a</span> <span style="color: #f92672">+</span> <span style="color: #f92672">[</span><span style="color: #f8f8f2">rand(</span><span style="color: #ae81ff">100</span><span style="color: #f8f8f2">)</span><span style="color: #f92672">]</span><span style="color: #f8f8f2">)</span><span style="color: #f92672">.</span><span style="color: #f8f8f2">join(</span><span style="color: #e6db74">", "</span><span style="color: #f8f8f2">)</span> <span style="color: #f92672">+</span> <span style="color: #e6db74">")"</span> <span style="color: #f8f8f2">}</span><span style="color: #f92672">.</span><span style="color: #f8f8f2">join(</span><span style="color: #e6db74">", "</span><span style="color: #f8f8f2">)</span>
<span style="color: #f92672">=></span> <span style="color: #e6db74">"(1, 2, 3, 98, 93), (4, 5, 6, 24, 92), (7, 8, 9, 77, 10)"</span>
<span style="color: #f8f8f2">irb(main):</span><span style="color: #ae81ff">006</span><span style="color: #f8f8f2">:</span><span style="color: #ae81ff">0</span><span style="color: #f92672">></span> <span style="color: #f8f8f2">array</span>
<span style="color: #f92672">=></span> <span style="color: #f92672">[[</span><span style="color: #ae81ff">1</span><span style="color: #f8f8f2">,</span> <span style="color: #ae81ff">2</span><span style="color: #f8f8f2">,</span> <span style="color: #ae81ff">3</span><span style="color: #f8f8f2">,</span> <span style="color: #ae81ff">98</span><span style="color: #f92672">]</span><span style="color: #f8f8f2">,</span> <span style="color: #f92672">[</span><span style="color: #ae81ff">4</span><span style="color: #f8f8f2">,</span> <span style="color: #ae81ff">5</span><span style="color: #f8f8f2">,</span> <span style="color: #ae81ff">6</span><span style="color: #f8f8f2">,</span> <span style="color: #ae81ff">24</span><span style="color: #f92672">]</span><span style="color: #f8f8f2">,</span> <span style="color: #f92672">[</span><span style="color: #ae81ff">7</span><span style="color: #f8f8f2">,</span> <span style="color: #ae81ff">8</span><span style="color: #f8f8f2">,</span> <span style="color: #ae81ff">9</span><span style="color: #f8f8f2">,</span> <span style="color: #ae81ff">77</span><span style="color: #f92672">]]</span>
</pre></div>
<br />
Thus, don’t forget that variables in ruby are passed by link and when you work with ruby iterators you work with the basic data.Anonymoushttp://www.blogger.com/profile/01888458412282498727noreply@blogger.com0