<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="https://www.w3.org/2005/Atom">
  <id>tag:rbjl.janlelis.com,2009:/atom</id>
  <link rel="alternate" type="text/html" href="https://localhost:3000"/>
  <link rel="self" type="application/atom+xml" href="https://rbjl.janlelis.com/atom?format=xml"/>
  <title>J-_-L</title>
  <updated>2015-05-03T14:02:40+02:00</updated>
  <entry>
    <id>tag:localhost,2009:Entry/72</id>
    <published>2015-05-03T14:02:40+02:00</published>
    <updated>2015-05-03T14:05:13+02:00</updated>
    <link rel="alternate" type="text/html" href="https://localhost:3000/72-idiosyncratic-ruby"/>
    <title>Idiosyncratic Ruby</title>
    <content type="html">&lt;p&gt;For the whole of May, I will blog about weird things in Ruby over at &lt;a href="https://idiosyncratic-ruby.com"&gt;Idiosyncratic Ruby&lt;/a&gt; &amp;#8211; Don&amp;#8217;t miss it!&lt;/p&gt;
CC-BY (DE)</content>
    <author>
      <name>Jan Lelis</name>
      <email>mail@janlelis.de</email>
    </author>
  </entry>
  <entry>
    <id>tag:localhost,2009:Entry/71</id>
    <published>2015-04-21T18:48:09+02:00</published>
    <updated>2015-04-22T01:00:55+02:00</updated>
    <link rel="alternate" type="text/html" href="https://localhost:3000/71-be-more-productive-with-better-sublime-snippets-for-ruby"/>
    <title>Be More Productive with Better Sublime Snippets for Ruby!</title>
    <content type="html">&lt;p&gt;Long time readers of this blog will remember that I used to tweak GNOME&amp;#8217;s gedit editor a lot. However, I ditched it for &lt;a href="https://sublimetext.com/"&gt;Sublime Text&lt;/a&gt; and I am not looking back. Here are some of the things I like about Sublime:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Offers a good out-of-the-box experience, including an amazing &amp;#8220;fuzzy search&amp;#8221; tool (ctrl+p)&lt;/li&gt;
	&lt;li&gt;It works on ubuntu without any problems&lt;/li&gt;
	&lt;li&gt;Fast (enough)&lt;/li&gt;
	&lt;li&gt;Multiple cursors!&lt;/li&gt;
	&lt;li&gt;Once you have installed &lt;a href="https://packagecontrol.io/"&gt;Package Control&lt;/a&gt;: An integrated extension repository&lt;/li&gt;
	&lt;li&gt;Encourages you to write your own extensions&lt;/li&gt;
	&lt;li&gt;Looks good&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Some of the things I dislike about Sublime:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Bad syntax highlighting for Ruby (more on this in a later blog post)&lt;/li&gt;
	&lt;li&gt;Default support for programming languages is often broken or outdated&lt;/li&gt;
	&lt;li&gt;Not happy with the default snippets for Ruby and &lt;span class="caps"&gt;ERB&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Introducing &amp;#8220;Productive Snippets for Ruby&amp;#8221;&lt;/h3&gt;
&lt;p&gt;The stock Ruby snippets are not particularly bad. But I had problems with the scope they cover. For example, they provide a snippet for &amp;#8220;transaction&amp;#8221;, which is very Railsish.  Also, a lot of snippets for Test::Unit are included. It has workarounds for 1.8 (e.g. getting singleton class), which are not needed any more (2.0 is the oldest officially support Ruby version). And all in all, you will notice a different style across all snippets.&lt;/p&gt;
&lt;p&gt;To repeat myself: I am not saying the default snippets suck. What I want ist to have a consistent selection of snippets with more &amp;#8220;modern&amp;#8221; idioms. And without support for specific gems, other than rake or bundler. This is, what the &lt;a href="https://github.com/janlelis/productive-sublime-snippets-ruby/"&gt;ProductiveSnippetsRuby package&lt;/a&gt; will give you. You can install it via sublime package control.&lt;/p&gt;
&lt;h3&gt;Removing the Default Snippets&lt;/h3&gt;
&lt;p&gt;The snippets are meant as a replacement for the default ones, but there is no simple way to remove them. You have to do it manually (at your own risk, of course):&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Find out where your sublime system packages folder is. This differs depending on your operationg system and how you installed sublime. For example, on my ubuntu machine it is &lt;code&gt;/opt/sublime_text/Packages&lt;/code&gt; (not &lt;code&gt;~/.config/sublime-text-3/Packages&lt;/code&gt;).&lt;/li&gt;
	&lt;li&gt;Look for a file called &lt;code&gt;Ruby.sublime-package&lt;/code&gt; and backup it&lt;/li&gt;
	&lt;li&gt;Then &lt;code&gt;unzip&lt;/code&gt; the file into an extra directory and change into that directory&lt;/li&gt;
	&lt;li&gt;Delete all &lt;code&gt;*.sublime-snippet&lt;/code&gt; files (or keep some that you like &amp;#8211; for example &lt;strong&gt;ProductiveSnippetsRuby&lt;/strong&gt; does currently not have snippets for all the &lt;code&gt;Enumerable&lt;/code&gt; methods).&lt;/li&gt;
	&lt;li&gt;&lt;code&gt;zip&lt;/code&gt; together the rest, call it &lt;code&gt;Ruby.sublime-package&lt;/code&gt; again and move it to the location of the original file&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;&lt;span class="caps"&gt;ERB&lt;/span&gt;&lt;/h3&gt;
&lt;p&gt;The default behaviour in &lt;span class="caps"&gt;ERB&lt;/span&gt; files is wrong, because some tab triggers (e.g. &lt;code&gt;if&lt;/code&gt;) generate php code. This is caused by an error in the &lt;code&gt;PHP.sublime-package&lt;/code&gt;. If you don&amp;#8217;t use &lt;span class="caps"&gt;PHP&lt;/span&gt;, the simplest way to get rid of the wrong snippets, will be to just delete the &lt;span class="caps"&gt;PHP&lt;/span&gt; package (or move it away). You can find it in the same directory where the &lt;code&gt;Ruby.sublime-package&lt;/code&gt; is (see last paragraph).&lt;/p&gt;
&lt;p&gt;Then install some &lt;a href="https://github.com/janlelis/productive-sublime-snippets-ruby/"&gt;Rails-free &lt;span class="caps"&gt;ERB&lt;/span&gt; snippets&lt;/a&gt; or use &lt;a href="https://github.com/matthewrobertson/ERB-Sublime-Snippets/"&gt;this alternative&lt;/a&gt;.&lt;/p&gt;
CC-BY (DE)</content>
    <author>
      <name>Jan Lelis</name>
      <email>mail@janlelis.de</email>
    </author>
  </entry>
  <entry>
    <id>tag:localhost,2009:Entry/70</id>
    <published>2015-03-12T21:25:54+01:00</published>
    <updated>2015-03-12T21:25:54+01:00</updated>
    <link rel="alternate" type="text/html" href="https://localhost:3000/70-get-your-microscope-for-micrologger-a-small-logger-on-top-of-microevent-rb"/>
    <title>Get your Microscope for MicroLogger: A small Logger on Top of MicroEvent.rb</title>
    <content type="html">&lt;p&gt;Being impressed by &lt;a href="https://github.com/jeromeetienne/microevent.js/blob/master/microevent.js#L12-31"&gt;MicroEvent.js&lt;/a&gt; I played with the thought to use such a library not only in JavaScript, but also in Ruby. The design pattern of this kind of message passing is called &lt;a href="https://en.wikipedia.org/wiki/Observer_pattern"&gt;observer pattern&lt;/a&gt; and there is also a library in &lt;a href="https://github.com/ruby/ruby/blob/trunk/lib/observer.rb"&gt;Ruby&amp;#8217;s standard library&lt;/a&gt; and some more on rubygems.org. However, &lt;strong&gt;MicroEvent.js&lt;/strong&gt; just nails it,&lt;/p&gt;
&lt;p&gt;so I rebuilt it in Ruby: &lt;a href="https://github.com/janlelis/microevent.rb/blob/master/lib/microevent.rb"&gt;MicroEvent.rb&lt;/a&gt; &amp;#8211; It is even shorter in Ruby, while having more features and more useful method return values than the JavaScript version. Ruby&amp;#8217;s procs/blocks are the closest equivalent to JavaScript functions, which has the advantage that there is no need to use Ruby&amp;#8217;s more complex methods.&lt;/p&gt;
&lt;p&gt;Here you can see it in action, working under the hood of a simple logger library:&lt;/p&gt;
&lt;div class="snippet-block"&gt;&lt;div class="snippet-path"&gt;&lt;a href="/70/micrologger_snippet.rb"&gt;micrologger_snippet&lt;/a&gt; &lt;/div&gt;&lt;div class="snippet snippet_ruby"&gt;&lt;a class="snippet_toggler" style="display:none" id="on_snippet_toggler_micrologger_snippetrb" title="toggle line numbers on" href="javascript:void(0)"&gt; &lt;/a&gt;&lt;pre class="ruby"&gt;&lt;pre&gt;&lt;span class="keyword"&gt;class &lt;/span&gt;&lt;span class="class"&gt;MicroLogger&lt;/span&gt;
  &lt;span class="ident"&gt;include&lt;/span&gt; &lt;span class="constant"&gt;MicroEvent&lt;/span&gt;

  &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;log&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;message&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;level&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="symbol"&gt;:info&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;extra&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="punct"&gt;{})&lt;/span&gt;
    &lt;span class="ident"&gt;trigger&lt;/span&gt; &lt;span class="ident"&gt;level&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;message&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="punct"&gt;{&lt;/span&gt;&lt;span class="ident"&gt;level&lt;/span&gt;&lt;span class="punct"&gt;:&lt;/span&gt; &lt;span class="ident"&gt;level&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;time&lt;/span&gt;&lt;span class="punct"&gt;:&lt;/span&gt; &lt;span class="constant"&gt;Time&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;now&lt;/span&gt;&lt;span class="punct"&gt;}.&lt;/span&gt;&lt;span class="ident"&gt;merge&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;extra&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
  &lt;span class="keyword"&gt;end&lt;/span&gt;

  &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;register&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;level&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="symbol"&gt;:info&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;handler&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="constant"&gt;nil&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="punct"&gt;&amp;amp;&lt;/span&gt;&lt;span class="ident"&gt;block&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
    &lt;span class="ident"&gt;bind&lt;/span&gt; &lt;span class="ident"&gt;level&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="punct"&gt;&amp;amp;&lt;/span&gt;&lt;span class="ident"&gt;resolve_handler&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;handler&lt;/span&gt; &lt;span class="punct"&gt;||&lt;/span&gt; &lt;span class="ident"&gt;block&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
  &lt;span class="keyword"&gt;end&lt;/span&gt;

  &lt;span class="comment"&gt;# ...&lt;/span&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Which is used like this:&lt;/p&gt;
&lt;div class="snippet-block"&gt;&lt;div class="snippet-path"&gt;&lt;a href="/70/micrologger_api.rb"&gt;micrologger_api&lt;/a&gt; &lt;/div&gt;&lt;div class="snippet snippet_ruby"&gt;&lt;a class="snippet_toggler" style="display:none" id="on_snippet_toggler_micrologger_apirb" title="toggle line numbers on" href="javascript:void(0)"&gt; &lt;/a&gt;&lt;pre class="ruby"&gt;&lt;pre&gt;&lt;span class="global"&gt;$logger&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="constant"&gt;MicroLogger&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;new&lt;/span&gt;
&lt;span class="global"&gt;$logger&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;register&lt;/span&gt; &lt;span class="symbol"&gt;:warn&lt;/span&gt; &lt;span class="keyword"&gt;do&lt;/span&gt; &lt;span class="punct"&gt;|&lt;/span&gt;&lt;span class="ident"&gt;message&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;_&lt;/span&gt;&lt;span class="punct"&gt;|&lt;/span&gt;
   &lt;span class="constant"&gt;STDERR&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;puts&lt;/span&gt; &lt;span class="ident"&gt;message&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;
&lt;span class="global"&gt;$logger&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;register&lt;/span&gt; &lt;span class="symbol"&gt;:warn&lt;/span&gt; &lt;span class="keyword"&gt;do&lt;/span&gt; &lt;span class="punct"&gt;|&lt;/span&gt;&lt;span class="ident"&gt;message&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;meta&lt;/span&gt;&lt;span class="punct"&gt;|&lt;/span&gt;
  &lt;span class="constant"&gt;File&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;open&lt;/span&gt;&lt;span class="punct"&gt;(&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;logfile.&lt;span class="expr"&gt;#{meta[:level]}&lt;/span&gt;.txt&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;,&lt;/span&gt; &lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;a&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;){&lt;/span&gt; &lt;span class="punct"&gt;|&lt;/span&gt;&lt;span class="ident"&gt;f&lt;/span&gt;&lt;span class="punct"&gt;|&lt;/span&gt; &lt;span class="ident"&gt;f&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;puts&lt;/span&gt; &lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;&lt;span class="expr"&gt;#{meta[:time]}&lt;/span&gt; | &lt;span class="expr"&gt;#{message}&lt;/span&gt;&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt; &lt;span class="punct"&gt;}&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;

&lt;span class="global"&gt;$logger&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;log&lt;/span&gt; &lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;hey&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;,&lt;/span&gt; &lt;span class="symbol"&gt;:warn&lt;/span&gt; &lt;span class="comment"&gt;# Will write to STDERR and logfile.warn.txt&lt;/span&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;You can find the full code &lt;a href="https://github.com/janlelis/micrologger"&gt;on github&lt;/a&gt;. I am open for feedback &lt;a href="https://twitter.com/JanLelis"&gt;on twitter&lt;/a&gt;.&lt;/p&gt;
CC-BY (DE)</content>
    <author>
      <name>Jan Lelis</name>
      <email>mail@janlelis.de</email>
    </author>
  </entry>
  <entry>
    <id>tag:localhost,2009:Entry/69</id>
    <published>2013-12-05T15:22:34+01:00</published>
    <updated>2013-12-05T15:22:34+01:00</updated>
    <link rel="alternate" type="text/html" href="https://localhost:3000/69-the-carb-stack-coffee-angular-rails-bower"/>
    <title>The CARB stack: Coffee + Angular + Rails + Bower</title>
    <content type="html">&lt;p&gt;&lt;a href="https://angularjs.org/"&gt;AngularJS&lt;/a&gt; is a great way to build modern web apps and this affects Rails programmers. The combination of both is an excellent choice: Build your single-page app in Angular and let it communicate via &lt;span class="caps"&gt;JSON&lt;/span&gt; with your &lt;a href="github.com/rails-api/rails-api"&gt;Rails-&lt;span class="caps"&gt;API&lt;/span&gt;&lt;/a&gt; backend (btw, this is exactly what we did &lt;a href="https://github.com/palavatv/palava-portal"&gt;for&lt;/a&gt; &lt;a href="https://github.com/palavatv/palava-hq"&gt;palava&lt;/a&gt;). However, you might ask yourself, what the best way to combine Angular and Rails is. Should you use the JavaScript tools world (grunt, yoeman, etc.) or should you prefer the Ruby tools (thor, sprockets)?&lt;/p&gt;
&lt;p&gt;Of course, this decision depends on you and your background. For me, the answer is clear: I want to stay in Ruby land. I am familiar with it and it works very well. Why should I throw the tools away for the same functionality, just because they are built in JavaScript? Don&amp;#8217;t get me wrong, you should try out &lt;a href="https://yeoman.io/"&gt;yoeman&lt;/a&gt; for a new angular project, it is fantastic. But nevertheless.. I like the Ruby stack. The &lt;i&gt;&lt;span class="caps"&gt;CARB&lt;/span&gt;&lt;/i&gt; stack.&lt;/p&gt;
&lt;h3&gt;What&amp;#8217;s out there?&lt;/h3&gt;
&lt;p&gt;Before I&amp;#8217;ll promote my own gem, a quick overview of projects with similar scope:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;&lt;a href="https://github.com/hiravgandhi/angularjs-rails"&gt;angularjs-rails&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href="https://github.com/rocky-jaiswal/ebenezer"&gt;Ebenezer&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href="https://github.com/FineLinePrototyping/angularjs-rails-resource"&gt;AngularJS Rails Resource&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href="https://github.com/patcito/angularjs_scaffold"&gt;angularjs_scaffold&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href="https://github.com/janders223/ng-rails"&gt;ng-rails&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;i&gt;angularjs-rails&lt;/i&gt; is an easy way to make Angular available from sprockets. However, I will use a more generic method to achieve this, so we won&amp;#8217;t need it.&lt;/p&gt;
&lt;p&gt;&lt;i&gt;Ebenezer&lt;/i&gt; is an approach to normally use Rails, but use the JavaScript stack at the same time. Check out the &lt;a href="https://rockyj.in/2013/10/24/angular_rails.html"&gt;blog&lt;/a&gt; &lt;a href="https://rockyj.in/2013/11/04/angular_rails_2.html"&gt;posts&lt;/a&gt;, if this sounds interesting to you.&lt;/p&gt;
&lt;p&gt;&lt;i&gt;AngularJS Rails Resource&lt;/i&gt; sits on the angular side. It makes communicating with your Rails backend super simple. Awesome!&lt;/p&gt;
&lt;p&gt;The bottom two projects are generators for working with Angular. I find generators extremely useful. Their purpose is not to automatically create tons of code, it is more about creating the right files in the right places with minimal boilerplate content to guide you. Especially the &lt;i&gt;ng-rails&lt;/i&gt; gem is designed very well!&lt;/p&gt;
&lt;p&gt;To conclude, what should an auxiliary gem do for you? In my opinion, it should:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;manage including AngularJS in Rails&lt;/li&gt;
	&lt;li&gt;contain generators&lt;/li&gt;
	&lt;li&gt;give me tools on the Angular side to work with Rails&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;It should enforce conventions, but leave the programming to the programmer.&lt;/p&gt;
&lt;h3&gt;Bower is simple&lt;/h3&gt;
&lt;p&gt;I am not fan of using bundler/gems to include a frontend JavaScript library, since it adds more complexity to dependency management. There are better solutions for managing frontend libraries and one of them is &lt;a href="https://bower.io"&gt;Bower&lt;/a&gt; by twitter. It is easy to use and super lightweight (some people even say, it is not much more than a fancy link shortener). While it does not have too much features, using it allows you to always have the overview of which versions of libraries you are running. I am often confused about this information when using a JavaScript library that comes out of a gem. And, of course, it is better than using plain JavaScript files without dependency management at all!&lt;/p&gt;
&lt;p&gt;This is the &lt;strong&gt;B&lt;/strong&gt; of &lt;i&gt;&lt;span class="caps"&gt;CARB&lt;/span&gt;&lt;/i&gt;.&lt;/p&gt;
&lt;h3&gt;Coffee is good&lt;/h3&gt;
&lt;p&gt;You should use &lt;a href="https://coffeescript.org/"&gt;CoffeeScript&lt;/a&gt;. It is popular amongst Ruby programmers. For a reason.&lt;/p&gt;
&lt;p&gt;This is the &lt;strong&gt;C&lt;/strong&gt; of &lt;i&gt;&lt;span class="caps"&gt;CARB&lt;/span&gt;&lt;/i&gt;.&lt;/p&gt;
&lt;h3&gt;Introducing Regular (Rails + Angular)&lt;/h3&gt;
&lt;p&gt;&lt;a href="https://github.com/janlelis/rg"&gt;Regular&lt;/a&gt; is my attempt to implement the &lt;i&gt;&lt;span class="caps"&gt;CARB&lt;/span&gt;&lt;/i&gt; stack. It depends on Bower to provide Angular. It comes with generators that promote a convention to organize your Angular project files. It generates Coffee. And it advices you to use &lt;a href="https://github.com/FineLinePrototyping/angularjs-rails-resource"&gt;AngularJS Rails Resource&lt;/a&gt;, because other developers have already thought about how to work with Rails from Angular.&lt;/p&gt;
CC-BY (DE)</content>
    <author>
      <name>Jan Lelis</name>
      <email>mail@janlelis.de</email>
    </author>
  </entry>
  <entry>
    <id>tag:localhost,2009:Entry/68</id>
    <published>2013-11-23T17:06:53+01:00</published>
    <updated>2013-11-23T17:08:23+01:00</updated>
    <link rel="alternate" type="text/html" href="https://localhost:3000/68-5-ruby-syntax-edge-cases"/>
    <title>5 Ruby Syntax Edge Cases</title>
    <content type="html">&lt;p&gt;My lightning talk from this year&amp;#8217;s eurucamp:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://janlelis.github.io/ruby-bad-parts/#2"&gt;The Bad Parts of the Ruby Programming Language&lt;/a&gt;&lt;/p&gt;
CC-BY (DE)</content>
    <author>
      <name>Jan Lelis</name>
      <email>mail@janlelis.de</email>
    </author>
  </entry>
  <entry>
    <id>tag:localhost,2009:Entry/67</id>
    <published>2013-01-30T14:52:47+01:00</published>
    <updated>2013-02-04T05:45:57+01:00</updated>
    <link rel="alternate" type="text/html" href="https://localhost:3000/67-ruby-and-random"/>
    <title>Ruby and Random</title>
    <content type="html">&lt;p&gt;Wrongly used randomness can be the source of hard-to-detect bugs and security holes. This is relevant every time you use randomness, for example, when implementing an existing protocol/interface that requires random values or generating tokens for your next raffle. This article describes when to use which of Ruby&amp;#8217;s randomness methods.&lt;/p&gt;
&lt;div class="snippet"&gt;&lt;pre&gt;&lt;code&gt;$ gem install forty_two
$ irb
&amp;gt;&amp;gt; require '42' # true
&amp;gt;&amp;gt; rand 100 # =&amp;gt; 42
&amp;gt;&amp;gt; [1,21,42,21,1].sample # =&amp;gt; 42&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;TL;DR&lt;/h3&gt;
&lt;p&gt;Don&amp;#8217;t use or rely on &lt;ins&gt;srand&lt;/ins&gt;, because of its global state. Generally use &lt;a href="https://www.ruby-doc.org/stdlib-1.9.3/libdoc/securerandom/rdoc/SecureRandom.html#random_number-method"&gt;SecureRandom&lt;/a&gt;, which is suitable for cryptographic purposes! If you need reproducible randomness, use your own &lt;a href="https://www.ruby-doc.org/core-1.9.3/Random.html"&gt;Random&lt;/a&gt; object!&lt;/p&gt;
&lt;h3&gt;Basic Ways to use Randomness in Ruby&lt;/h3&gt;
&lt;p&gt;Generating random values in Ruby is convenient:&lt;/p&gt;
&lt;h4&gt;Creating Numbers&lt;/h4&gt;
&lt;div class="snippet"&gt;&lt;pre&gt;&lt;code&gt;rand 10     # integer between 0..9
rand 2..5   # integer between 2..5
rand        # float between 0 and 1
Random.rand # basically the same like rand&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h4&gt;Selecting Array Elements&lt;/h4&gt;
&lt;div class="snippet"&gt;&lt;prc&gt;&lt;code&gt;[1,2,3,4,5].sample   # picks one element
[1,2,3,4,5].sample 3 # picks a subset of three elements
[1,2,3,4,5].shuffle  # reorders the array&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h4&gt;The Catch&lt;/h4&gt;
&lt;p&gt;But all the above methods have one thing in common: They depend on the same global seed. The seed is a non-random number needed to initialize the &lt;a href="https://en.wikipedia.org/wiki/Pseudorandom_number_generator" title="prng"&gt;pseudorandom number generator&lt;/a&gt; used by Ruby (a &lt;a href="https://en.wikipedia.org/wiki/Mersenne_twister"&gt;Mersenne Twister&lt;/a&gt;). These number generators have been around for a while and work well (&lt;a href="https://spideroak.com/blog/20121205114003-exploit-information-leaks-in-random-numbers-from-python-ruby-and-php"&gt;they are not unbreakable, though&lt;/a&gt;). The seeds are typically made out of values like the process number or the current time in nano seconds. The global seed is created when the Ruby process starts.&lt;/p&gt;
&lt;p&gt;However, it&amp;#8217;s also possible to set your own seed using the &lt;ins&gt;srand&lt;/ins&gt; or the &lt;code&gt;Random&lt;/code&gt;.&lt;ins&gt;srand&lt;/ins&gt; method. You should not do this. Furthermore, you should not rely on methods that depend on &lt;ins&gt;srand&lt;/ins&gt; at all, because the seed is a global state and gets shared between all libraries loaded.&lt;/p&gt;
&lt;p&gt;Because of Ruby&amp;#8217;s nature, it&amp;#8217;s not uncommon that a single library can change (and potentially break) the behavior of a Ruby application. But you should consider that the use of &lt;ins&gt;srand&lt;/ins&gt; (instead of the newer approach below) could have happened without malicious intentions, which still might open a door for attackers. If your application uses any &lt;ins&gt;srand&lt;/ins&gt;-based method (e.g. &lt;code&gt;Array&lt;/code&gt;&lt;ins&gt;#sample&lt;/ins&gt;) and you care about the quality of your randomness, you will have to check all the gems in your &lt;code&gt;Gemfile.lock&lt;/code&gt; for the use of &lt;ins&gt;srand&lt;/ins&gt;.&lt;/p&gt;
&lt;h4&gt;Time Machine&lt;/h4&gt;
&lt;p&gt;By the way, another feature of &lt;ins&gt;srand&lt;/ins&gt; is that it returns the previously used seed. This might be useful to calculate a new seed based on the old one. On the other hand, it (theoretically) allows an attacker to &amp;#8220;steal&amp;#8221; your previously used randomness:&lt;/p&gt;
&lt;div class="snippet"&gt;&lt;prc&gt;&lt;code&gt;$ irb -f
&amp;gt;&amp;gt; def generate_secret_token
&amp;gt;&amp;gt;   (0..16).map{[*33..127].sample.chr}.join
&amp;gt;&amp;gt; end # =&amp;gt; nil
&amp;gt;&amp;gt; File.write 'secret_token_file', generate_secret_token # writes e.g. "5^hh}kq&amp;amp;294']rQ.|"
&amp;gt;&amp;gt; # other stuff is going on
&amp;gt;&amp;gt; # ...
&amp;gt;&amp;gt; # evil coworker accesses your irb
&amp;gt;&amp;gt; srand srand # capture old seed and use it again
&amp;gt;&amp;gt; generate_secret_token # =&amp;gt; "a5^hh}kq&amp;amp;294']rQ."&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;The Proper Ways to use Randomness in Ruby&lt;/h3&gt;
&lt;h4&gt;&lt;code&gt;SecureRandom&lt;/code&gt;&lt;/h4&gt;
&lt;p&gt;This should be your favorite way of creating a random number:&lt;/p&gt;
&lt;div class="snippet"&gt;&lt;prc&gt;&lt;code&gt;require 'securerandom'
SecureRandom.random_number # =&amp;gt; ...&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;It also provides the useful &lt;ins&gt;.random_bytes&lt;/ins&gt; and &lt;ins&gt;.uuid&lt;/ins&gt; methods. &lt;code&gt;SecureRandom&lt;/code&gt; works different than the usual randomness methods, since it uses external libraries to generate the randomness. At first, it tries to use &lt;a href="https://www.openssl.org/docs/crypto/rand.html"&gt;OpenSSL&lt;/a&gt; and if that&amp;#8217;s not available, it will try &lt;a href="https://www.kernel.org/doc/man-pages/online/pages/man4/random.4.html"&gt;/dev/urandom&lt;/a&gt; or &lt;a href="https://www.processlibrary.com/directory/files/advapi32/22015/"&gt;advapi32&lt;/a&gt;, which are provided by your operating system. It does not have the &lt;ins&gt;srand&lt;/ins&gt; problem and &lt;a href="https://stackoverflow.com/questions/12118406/is-rubys-seed-for-opensslrandom-sufficient"&gt;is considered to be strong enough for cryptographic purposes&lt;/a&gt;. So why not use it everywhere?&lt;/p&gt;
&lt;p&gt;Since the &lt;span class="caps"&gt;API&lt;/span&gt; is not as convenient as the normal randomness methods (e.g. no array core extensions), you can help yourself by &lt;a href="https://coderwall.com/p/oijyja"&gt;building or using simple wrappers&lt;/a&gt;!&lt;/p&gt;
&lt;h4&gt;Random.new&lt;/h4&gt;
&lt;p&gt;Sometimes, you face a different problem: You might want to generate a random sequences that can be reproduced later. You don&amp;#8217;t need to use &lt;ins&gt;srand&lt;/ins&gt; to achieve this, anymore. Newer Ruby versions fix this global state problem by including the &lt;code&gt;Random&lt;/code&gt; class that allows you to create instances that represent the state of the prng in an object oriented way. It is used like this:&lt;/p&gt;
&lt;div class="snippet"&gt;&lt;prc&gt;&lt;code&gt;r = Random.new 84
r.rand 100
[1,2,3,4,5].sample(random: r)
[1,2,3,4,5].shuffle(random: r)&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Pay attention to not do &lt;code&gt;[1,2,3,4,5].shuffle(random: Random.new)&lt;/code&gt; &amp;#8211; Creating new seeds all the time is less secure than using new numbers out of an existing prng. In these cases, you should use &lt;code&gt;SecureRandom&lt;/code&gt; (see above) instead.&lt;/p&gt;
CC-BY (DE)</content>
    <author>
      <name>Jan Lelis</name>
      <email>mail@janlelis.de</email>
    </author>
  </entry>
  <entry>
    <id>tag:localhost,2009:Entry/66</id>
    <published>2012-12-03T01:26:29+01:00</published>
    <updated>2012-12-03T01:33:25+01:00</updated>
    <link rel="alternate" type="text/html" href="https://localhost:3000/66-activesupport-4-by-example-numeric-to_s"/>
    <title>ActiveSupport 4 by Example: Numeric#to_s</title>
    <content type="html">&lt;p&gt;The &lt;a href="https://railsapi.com/doc/rails-v3.2.6/classes/ActionView/Helpers/NumberHelper.html"&gt;NumberHelper&lt;/a&gt; functionality is now available as &lt;code&gt;Numeric#to_s&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;To try it out, do&lt;/p&gt;
&lt;div class="snippet"&gt;&lt;pre&gt;&lt;code&gt;git clone https://github.com/rails/rails
cd rails/activesupport
rvm gemset use rails4 --create
rake gem
gem install pkg/activesupport-4.0.0.beta
irb
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class="snippet-block"&gt;&lt;div class="snippet-path"&gt;&lt;a href="/66/as4_number_helper.rb"&gt;as4_number_helper&lt;/a&gt; &lt;/div&gt;&lt;div class="snippet snippet_ruby"&gt;&lt;a class="snippet_toggler" style="display:none" id="on_snippet_toggler_as4_number_helperrb" title="toggle line numbers on" href="javascript:void(0)"&gt; &lt;/a&gt;&lt;pre class="ruby"&gt;&lt;pre&gt;&lt;span class="comment"&gt;# from https://github.com/rails/rails/blob/master/activesupport/lib \&lt;/span&gt;
&lt;span class="comment"&gt;# /active_support/core_ext/numeric/conversions.rb&lt;/span&gt;
&lt;span class="punct"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="ident"&gt;require&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;active_support/core_ext/numeric/conversions&lt;/span&gt;&lt;span class="punct"&gt;'&lt;/span&gt; &lt;span class="comment"&gt;#=&amp;gt; true&lt;/span&gt;
&lt;span class="punct"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="number"&gt;42&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;to_s&lt;/span&gt; &lt;span class="comment"&gt;#=&amp;gt; &amp;quot;42&amp;quot;&lt;/span&gt;
&lt;span class="punct"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="number"&gt;13&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;to_s&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="symbol"&gt;:rounded&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;precision&lt;/span&gt;&lt;span class="punct"&gt;:&lt;/span&gt; &lt;span class="number"&gt;5&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt; &lt;span class="comment"&gt;#=&amp;gt; &amp;quot;13.00000&amp;quot;&lt;/span&gt;
&lt;span class="punct"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="number"&gt;12345678&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;to_s&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="symbol"&gt;:delimited&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt; &lt;span class="comment"&gt;#=&amp;gt; &amp;quot;12,345,678&amp;quot;&lt;/span&gt;
&lt;span class="punct"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="number"&gt;98765432.98&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;to_s&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="symbol"&gt;:delimited&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;delimiter&lt;/span&gt;&lt;span class="punct"&gt;:&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;.&lt;/span&gt;&lt;span class="punct"&gt;',&lt;/span&gt; &lt;span class="ident"&gt;separator&lt;/span&gt;&lt;span class="punct"&gt;:&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;,&lt;/span&gt;&lt;span class="punct"&gt;')&lt;/span&gt;
&lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;98.765.432,98&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;
&lt;span class="punct"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="number"&gt;55.22&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;to_s&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="symbol"&gt;:percentage&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;precision&lt;/span&gt;&lt;span class="punct"&gt;:&lt;/span&gt; &lt;span class="number"&gt;1&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt; &lt;span class="comment"&gt;#=&amp;gt; &amp;quot;55.2%&amp;quot;&lt;/span&gt;
&lt;span class="punct"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="number"&gt;1234567890123456&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;to_s&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="symbol"&gt;:human&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt; &lt;span class="comment"&gt;#=&amp;gt; &amp;quot;1.23 Quadrillion&amp;quot;&lt;/span&gt;
&lt;span class="punct"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="number"&gt;1234567&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;to_s&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="symbol"&gt;:human_size&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;precision&lt;/span&gt;&lt;span class="punct"&gt;:&lt;/span&gt; &lt;span class="number"&gt;2&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt; &lt;span class="comment"&gt;#=&amp;gt; &amp;quot;1.2 MB&amp;quot;&lt;/span&gt;
&lt;span class="punct"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="number"&gt;1235551234&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;to_s&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="symbol"&gt;:phone&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt; &lt;span class="comment"&gt;#=&amp;gt; &amp;quot;123-555-1234&amp;quot;&lt;/span&gt;
&lt;span class="punct"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="number"&gt;1235551234&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;to_s&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="symbol"&gt;:phone&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;delimiter&lt;/span&gt;&lt;span class="punct"&gt;:&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt; &lt;/span&gt;&lt;span class="punct"&gt;',&lt;/span&gt; &lt;span class="ident"&gt;area_code&lt;/span&gt;&lt;span class="punct"&gt;:&lt;/span&gt; &lt;span class="constant"&gt;true&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
&lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;(123) 555 1234&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;
&lt;span class="punct"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="number"&gt;1234567890.50&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;to_s&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="symbol"&gt;:currency&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt; &lt;span class="comment"&gt;#=&amp;gt; &amp;quot;$1,234,567,890.50&amp;quot;&lt;/span&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
CC-BY (DE)</content>
    <author>
      <name>Jan Lelis</name>
      <email>mail@janlelis.de</email>
    </author>
  </entry>
</feed>
