\r\n\r\n","bodyAsHTML":"
Unsorted list of features, howto's, suggar and evils of ruby (on rails)\r\n\r\n\r\n# THIS FILE CONTAINS INTERESTING SNIPPETS OF RUBY/RAILS\r\n# in order to accelerate learning and find forgotten things\r\n#  \r\n#  Key shortcuts!\r\n#  ctrl+shift+v  controller/view!\r\n#  ctrl+k  find next word\r\n#  ctrl+m maximize editor\r\n#  \r\n# \r\n# \r\n#   \r\nBAD\r\n-----------------------\r\nmodel files do not contain the classes field information \r\ninstead this is stored in different 'migration' files\r\ncreate_table \"periods\" do |t|\r\n    t.column \"target_id\", :integer\r\n    t.column \"target_type\", :string\r\n    t.column \"start_at\", :datetime\r\n    t.column \"end_at\", :datetime\r\n    t.column \"wholeday\", :boolean\r\n    t.column \"wholeweek\", :boolean\r\n    t.column \"allweeks\", :boolean\r\n  end\r\n NO inbuild multi dimensional arrays\r\nDANGER!!!!!\r\nand is not && !!!!!!!!!!!!!!!!!!!!!!!!\r\nreturn x==7 and y==8  => void value expression!!!\r\nreturn x==7 && y==8  !!!\r\nnr==0 (evaluates string.nil? !!!!!!!  \"0\"==0 -> false!!!!)\r\nnr.to_i==0 !!\r\n\r\n#ANNOYANCES:\r\n+ operator for string is really baad :\r\ncannot convert nil into String !!! \r\nputs \"teacher \"+ @lesson.teacher\r\n \"last saved : \"+ DateTime.now.to_s  #to_s !!! neccessary!!!!!!! (and ugly)\r\n\"nr\"+rand(10).to_s#!!!\r\n\r\nLanguage buggy: (see http://public.transcraft.co.uk/blog/index.html)\r\nActionController::RoutingError (Recognition failed for \"/images/btnbg.gif\"):\r\n\r\nhelpers are only for views!! (ApplicationController is what you need)\r\n\r\n@links = Link.find_by_sql [\r\n\"SELECT * from links where link like '\"+ @a +\"%' order by id asc\"]\r\n  #endwith startwith\r\ns = \"Hello, world\"\r\n/^Hello/.match( s ) \t>> true\r\n/world$/.match( s ) \t\t>> true\r\n  \r\n \r\n\r\n  \r\n  -------------------------------------\r\nGood\r\n********************\r\n\r\nclass.byname:\r\n\r\n  alle= eval(type+\".find(:all)\")\r\n  Object.const_get(\"Array\")\r\n\r\n# default values\r\ndef initialize(definitions=[], word = nil)       \r\n      @definitions = definitions\r\n      @word = word\r\n    end\r\n\r\n\r\nrake db_schema_dump !!! instead of writing the migration files yourself\r\n\r\nrake --help\r\nrake migrate VERSION=n  #runs from 0 to n !!!!!!!!!!!\r\n\r\n\r\nCustomer.find_all [ \"company = ?\", company ]\r\nund wenn man eine LIKE '%xx%' Abfrage erstellen möchte:\r\nCustomer.find_all [ \" company LIKE ? \", '%#{company}%' ]\r\n\r\nstring arrays:\r\nx= %w(Montag Dienstag) is short for \r\ny= [\"Montag\" ,\"Dienstag\"]\r\n\r\n<%= sortable_element('listOrTableId' , :url => { :action => 'move' }) #, :update => 'test-id',:tag => 'tr',  :complete => visual_effect(:highlight, q.id))%>\r\n\r\n<%= observe_field(:search,\r\n\t:frequency => 0.5,\r\n\t:update => :results,\r\n\t:url => { :action => :search }, \r\n\t:loading => \"Element.show('indicator_gif_id')\",\r\n\t:complete => \"Element.hide('indicator_gif_id')\") \r\n\t%>\r\n\r\n\r\n/// HOW TOs  (mixed with 'good')\r\n\r\nglobal functions !\r\nput them in application.rb (ApplicationController)\r\n\r\n\r\n\r\n\r\nRegular expressions\r\n#substitute patterns (m=LineBreaks u=Unicode i=IgnoreCase)\r\na=a.gsub(//mui, \"\")  # yeah right nice naming, suckers! \r\n\r\nsend_file or send http response data\r\n  send_data @person.picture, :filename => @person.filename, :type => \"image/jpeg\", :disposition => \"inline\"\r\n\r\nrequire 'net/ftp'\r\n ftp = Net::FTP.new('ftp.netlab.co.jp') \r\n ftp.login files = ftp.chdir('pub/lang/ruby/contrib')\r\n  files = ftp.list('n*') \r\n  ftp.getbinaryfile('nif.rb-0.91.gz', 'nif.gz', 1024)\r\n   ftp.close\r\n\r\n#reflection : \r\nlist.class.reflect_on_all_associations\r\n\r\n>>>>>>>regex<<<<<<<<\r\n// greedy !!!!\r\nputs \"a[bcb]d[bebb]rerberb]w[bsdaeb]ega\".scan(/\\[b(.*)b/).inspect\r\n=>cb]d[bebb]rerberb]w[bsdae\r\n\r\n// .*? not greedy ++\r\nputs \"a[bcb]d[bebb]rerberb]w[bsdaeb]egab\".scan(/\\[b(.*?)b/).inspect\r\n=> [[\"c\"], [\"e\"], [\"sdae\"]]\r\n\r\n#debug over ssh:\r\n#ruby breakpointer [options] [server uri]\r\n#ruby script/generate scaffold x // model + view!\r\n#http://www.rubycentral.com/book/tut_modules.html modules\r\n\r\n\r\n  require 'net/http'\r\n    require 'uri'\r\n    img='http://www.macdesktops.com/?res=TRUE&category=35'\r\n      html=Net::HTTP.get URI.parse(img)\r\n\r\n#switch cases\r\n def search\r\n    @results = Search.find(params[:query])\r\n    case @results\r\n      when 0 then render :action=> \"no_results\"\r\n      when 1 then render :action=> \"show\"\r\n      when 2..10 then render :action=> \"show_many\"\r\n    end\r\n  end\r\n  \r\n\r\nmodule Action\r\n  VERY_BAD = 0\r\n  BAD      = 1\r\n  def Action.sin(badness)\r\n    # ...\r\n  end\r\nend\r\n\r\nrequire \"trig\"\r\nrequire \"action\"\r\n##########\r\nMIXINS\r\nmodule Debug\r\n  def whoAmI?\r\n    \"#{self.type.name} (\\##{self.id}): #{self.to_s}\"\r\n  end\r\nend\r\nclass Phonograph\r\n  include Debug\r\n  # ...\r\nend\r\nclass EightTrack\r\n  include Debug\r\n  # ...\r\nend\r\nph = Phonograph.new(\"West End Blues\")\r\net = EightTrack.new(\"Surrealistic Pillow\")\r\n#ph.whoAmI? \t» \t\"Phonograph (#537766170): West End Blues\"\r\n#et.whoAmI? \t» \t\"EightTrack (#537765860): Surrealistic Pillow\"\r\n##########################33\r\ntime=5.minutes + 30.seconds ## cool !!!!!!\r\n\r\n5.times do |i|\r\n   File.open(\"temp.rb\",\"w\") { |f|\r\n     f.puts \"module Temp\\ndef Temp.var() #{i}; end\\nend\"\r\n   }\r\n   load \"temp.rb\"\r\n   puts Temp.var\r\n end\r\n ################\r\nprint \"Enter your name: \"\r\nname = gets\r\n#################\r\n\r\nArgumentlisten variabler Länge\r\nWas aber, wenn Sie eine variable Anzahl von Argumenten übergeben wollen oder mehrere Argumente mit einem einzigen Parameter auffangen wollen? Wenn Sie einen Stern vor den Namen des Parameters nach den ``normalen'' Parametern setzen, wird genau das gemacht.\r\ndef varargs(arg1, *rest)\r\n  \"Got #{arg1} and #{rest.join(', ')}\"\r\nend\r\n\r\n\r\ny = Trig.sin(Trig::PI/4)\r\nwrongdoing = Action.sin(Action::VERY_BAD)\r\n# setup:\r\n# rails \r\n# ruby script\\generate scaffold survey #!!  does the following too:\r\n# ruby script\\generate controller survey\r\n# ruby script\\generate model survey\r\n#backward if\r\n#def isanum=true\r\nputs \"It's not a number\" if isanum.nil?\r\n\r\n#$! variable!!\r\n rescue ScriptError, StandardError\r\n    printf \"ERR: %s\\n\", $! || 'exception raised'\r\n#eval\r\neval(line).inspect\r\n\r\n#regexp matching   & no brackets\r\nif a =~ /^[-+]?\\d+$/;\r\n\tputs \"It's a number that may indicate positive or negative\"\r\nend\r\n\r\nDoes anyone know how to get an ID back, instead of a String?\r\n@doctor = Doctor.find_by_name(params[:doctor][:name])\r\nparams[:parent][:doctor] = @doctor\r\n\r\n#what is that?   yml intralanguage :(\r\n#  database: verwurzelt_de_development\r\n#  <<: *login\r\n\r\n\r\n#Simplemost file IO:\r\nIO.foreach(\"testfile\") { |line| puts line }\r\n\r\narr = IO.readlines(\"testfile\")\r\n\r\n#Simple socket\r\nrequire 'socket'\r\nclient = TCPSocket.open('localhost', 'finger')\r\nclient.send(\"oracle\\n\", 0)    # 0 means standard packet\r\nputs client.readlines\r\nclient.close\r\n\r\n#HTTP\r\nrequire 'net/http'\r\nh = Net::HTTP.new('www.pragmaticprogrammer.com', 80)\r\nresp, data = h.get('/index.html', nil)\r\nif resp.message == \"OK\"\r\n  data.scan(/\r\n// 'question',:action=>'move'%>\";\r\n\tnew Ajax.Request(url,{parameters:data});\r\n    }\r\n  });\r\n// ]]>\r\n\r\n\r\n
","author":{"id":1018055,"username":"Snippets_User","realname":null,"emailHash":"7c2a6e78db5bc42c725740ccc54d15b6","avatar":"https://secure.gravatar.com/avatar/7c2a6e78db5bc42c725740ccc54d15b6?d=identicon&r=PG","reputation":0},"activeRevisionId":989933,"revisionIds":[989933],"lastActiveUserId":1018055,"lastActiveDate":1151544468000,"parentId":null,"parentAuthor":null,"originalParentId":null,"childrenIds":[],"commentIds":[],"marked":true,"topics":[],"primaryContainerId":8,"containerIds":[],"plug":"ruby-language-features","wiki":false,"score":0,"depth":0},"enableThreadedComments":true,"contentType":"article"}],"loadedStyles":[["/lib/bootstrap/bootstrap.less","/lib/font-awesome/font-awesome.less","/lib/fontello/css/fontello.css","/lib/fontello/css/animation.css","/lib/angular-ui/select.css","/lib/ngDialog/css/ngDialog.css","/less/ngDialog-theme.less","/lib/bootstrap-switch/bootstrap-switch.css","/less/dzone20.less","/less/fonts.less","/less/directives.less","/lib/slick/slick.css","/lib/bootstrap-slider/bootstrap-slider.css","/less/layout.less","/widgets/article/content/article-content.less","/widgets/article/infoBar/widget.less","/widgets/components/slider/widget.less","/widgets/content/commentsSlider/widget.less","/widgets/header/blackBar/widget.less","/widgets/header/main/header-common.less","/widgets/header/main/widget.less","/widgets/sidebar/content/list/list.less","/widgets/sidebar/tapBar/sidebar-list.less","/widgets/sidebar/tapBar/widget.less","/widgets/users/UserHomeMiniProfile/widget.less"]],"loadedScripts":[["/lib/jquery/jquery.js","/lib/lodash/lodash.js","/lib/moment/moment.js","/scripts/utils.js","/lib/angular/angular.js","/lib/angular/angular-sanitize.js","/lib/local-storage/angular-local-storage.js","/lib/bootstrap/bootstrap.js","/lib/angular-ui/bootstrap.js","/lib/angular-ui/select.js","/lib/bootstrap-switch/bootstrap-switch.js","/lib/ngDialog/js/ngDialog.js","/lib/angular-moment/angular-moment.js","/scripts/app.js","/scripts/socket.js","/scripts/services.js","/scripts/ui-services.js","/scripts/directives.js","/scripts/filters.js","/lib/angular-touch/angular-touch.min.js","/lib/elastic/elastic.js","/lib/ng-file-upload/angular-file-upload-all.js","/lib/angular-deckgrid/angular-deckgrid.js","/scripts/dzone.js","/scripts/ads.js","/scripts/head.js","/scripts/links.js","/scripts/utilities/directives.js","/scripts/utilities/services.js","/scripts/utilities/image-editor.js","/lib/bootstrap-slider/bootstrap-slider.js","/lib/bootstrap-slider/directive.js","/lib/angular-draganddrop/draganddrop.js","/widgets/article/content/utils.js","/widgets/article/infoBar/services.js","/widgets/components/slider/service.js","/widgets/header/main/angulartics-ga.js","/widgets/header/main/angulartics.js","/widgets/header/main/resize.js","/widgets/sidebar/content/list/service.js","/widgets/sidebar/tapBar/directive.js","/widgets/sidebar/tapBar/service.js"]],"TH_CSRF":"6728250711360195019","botInfo":[{"isRenderBot":false}],"request":[{"site":{"id":7,"title":"DZone: Programming & DevOps news, tutorials & tools","keywords":"programming, software development, devops, java, agile, web, iot, database, mobile, big data, cloud","description":"Programming, Web Development, and DevOps news, tutorials and tools for beginners to experts. Hundreds of free publications, over 1M members, totally free.","name":"DZone.com"},"dev":false,"cdn":["dz2cdn1.dzone.com","dz2cdn2.dzone.com","dz2cdn3.dzone.com","dz2cdn4.dzone.com"],"theme":"dz20","context":"","user":{"id":2500002,"authenticated":false,"name":"Anonymous","realName":null,"avatar":"https://secure.gravatar.com/avatar/?d=identicon&r=PG","profile":"/users/2500002/anon-user.html"}}],"portals":[[{"topic":8,"id":2,"shortTitle":"agile-methodology-training-tools-news","color":"red","name":"Agile","code":"agile","url":"/agile-methodology-training-tools-news"},{"topic":6129,"id":3,"shortTitle":"big-data-analytics-tutorials-tools-news","color":"green","name":"Big Data","code":"big-data","url":"/big-data-analytics-tutorials-tools-news"},{"topic":30,"id":4,"shortTitle":"cloud-computing-tutorials-tools-news","color":"orange","name":"Cloud","code":"cloud","url":"/cloud-computing-tutorials-tools-news"},{"topic":59,"id":5,"shortTitle":"database-sql-nosql-tutorials-tools-news","color":"purple","name":"Database","code":"database","url":"/database-sql-nosql-tutorials-tools-news"},{"topic":31,"id":6,"shortTitle":"devops-tutorials-tools-news","color":"yellow","name":"DevOps","code":"devops","url":"/devops-tutorials-tools-news"},{"topic":1138,"id":7,"shortTitle":"enterprise-integration-training-tools-news","color":"green","name":"Integration","code":"integration","url":"/enterprise-integration-training-tools-news"},{"topic":48,"id":8,"shortTitle":"iot-developer-tutorials-tools-news-reviews","color":"orange","name":"IoT","code":"iot","url":"/iot-developer-tutorials-tools-news-reviews"},{"topic":1,"id":1,"shortTitle":"java-jdk-development-tutorials-tools-news","color":"purple","name":"Java","code":"java","url":"/java-jdk-development-tutorials-tools-news"},{"topic":29,"id":9,"shortTitle":"mobile-app-developer-tutorials-tools-news","color":"yellow","name":"Mobile","code":"mobile","url":"/mobile-app-developer-tutorials-tools-news"},{"topic":653,"id":10,"shortTitle":"apm-tools-performance-monitoring-optimization","color":"red","name":"Performance","code":"performance","url":"/apm-tools-performance-monitoring-optimization"},{"topic":35,"id":11,"shortTitle":"web-development-programming-tutorials-tools-news","color":"orange","name":"Web Dev","code":"webdev","url":"/web-development-programming-tutorials-tools-news"}]],"matchedUrl":{"name":"dzone:articles:view","mapping":"/articles/**","mappingPatterns":{}},"requiresModule":["angulartics","angulartics.google.analytics","dndLists","generalDirectives","monospaced.elastic","angularFileUpload","akoenig.deckgrid","ui.bootstrap-slider","ngSanitize","ui.select","ui.bootstrap","angularMoment","ngTouch","ngDialog","LocalStorageModule"]}; } catch(e) { console.error(e); }
4
Notification
  • Adam Van Grack started following you

  • Jessica Conaghan, Gumption Scott and 6 commented on a link you posted

    T-Mobile Unvells Cheaper, More Basic 'Simple Prepaid' Phone Plans

  • Jessica Conaghan replied on a comment you posted

    CSS counters are one of those "oh neat, didn't know CSS could do that" features with a lot of Xubuntu 14.10 Utopic Unicorn is the latest release of xubuntu based on Ubuntu 14.10

  • Jessica Conaghan started following you

See All
POWERED BY DZone TeamHub
\r\n\r\n","source":null,"views":9,"articleDate":1151544468000,"tldr":null,"originalSource":"","published":true,"nComments":0,"articleType":"snippet"}]; WMODEL_DATA.perms = {"canDecidePick":false,"canPublish":false}; WMODEL_DATA.isPreview = false; WMODEL_DATA.OPTIONS = {}; TH.installWidgetController('article.content', 'articleContent7', WMODEL_DATA, typeof controller == 'function' ? controller : null, [{name: 'partners', data: true},{name: 'DEFAULT', data: true}], ' oUhbblYOaqbcblYOaqbcC', null); })(); (function() { function controller($scope, $service, $location, SideBarService, TH$Request) { SideBarService.ctx.pageSize = $scope.pageSize; SideBarService.ctx.isPreview = $scope.isPreview; SideBarService.ctx.mode = $scope.mode; SideBarService.fn.loader = $service; SideBarService.fn.scrollCheck = function() { $scope.$emit('thIfScrollCheck'); }; var currentFilter; $scope.$on('$locationChangeSuccess', function() { if (!$location.search().filter) { $scope.filter = 'latest'; } else { $scope.filter = $location.search().filter; if ($scope.filter == 'latest') { $location.search('filter', null); } } if (currentFilter == $scope.filter) { return; } currentFilter = $scope.filter; SideBarService.ctx.filter = $scope.filter; }); $scope.display = SideBarService.getList(); $scope.$watchCollection(function() { return SideBarService.getList(); }, function (n) { $scope.display = n; }); $scope.isActive = SideBarService.isActive; $scope.isExcluded = SideBarService.isExcluded; $scope.loadMore = SideBarService.load; $scope.loading = function() { return SideBarService.ctx.loading; } } var WMODEL_DATA = {}; WMODEL_DATA.pageSize = 20; WMODEL_DATA.isPreview = false; WMODEL_DATA.OPTIONS = {}; WMODEL_DATA.mode = null; TH.installWidgetController('sidebar.content.list', 'sidebarContentList10', WMODEL_DATA, typeof controller == 'function' ? controller : null, [{name: 'DEFAULT', data: true}], ' oUhbkSMaaqbcdvVkcC', null); })(); (function() { var WMODEL_DATA = {}; WMODEL_DATA.OPTIONS = {}; TH.installWidgetController('sidebar.tapBar', 'sidebar', WMODEL_DATA, typeof controller == 'function' ? controller : null, null, ' oUhbkSMadabfWVcC oUhbkSMadabbWQbVkcC', null); })(); (function() { function controller($scope, shareThis, TH$Dialog, TH$Service) { $scope.getEditUrl = function(id, type) { if (!type || type == 'article') { return '/content/' + id + '/edit.html'; } else { return '/dzone/staff/' + type + (type == 'refcard' ? 'z' : 's') + '/' + id + '/edit.html'; } } $scope.share = function(socialNet, url, title){ shareThis.shareThis(socialNet, url, title); }; $scope.canDelete = function(article) { return article.canDelete; } $scope.edit = function(link) { TH$Dialog.open({ loadWidget: 'links.postPreview', widgetArgs: { edit: link.id }, size: 'xbig' }).then(function (result) { $scope.link.title = result.title; $scope.link.linkDescription = result.content; $scope.link.thumb = result.thumb; $scope.link.tags = result.topics; }); }; $scope.deleteLink = function(article) { var title = article.title; var type = 'link'; if(article.header){ title = (article.header.type == 'article') ? article.header.title : article.title; type = (article.header.type == 'article') ? 'article' : 'link'; } TH$Dialog.confirm('Do you want to delete "' + title + '"?').then(function() { return TH$Service.action('delete', {type: type, id: article.id}); }).then(function() { article.deleted = true; }); }; } var WMODEL_DATA = {}; WMODEL_DATA.OPTIONS = {}; TH.installWidgetController('content.commentsSlider', 'contentCommentsSlider9', WMODEL_DATA, typeof controller == 'function' ? controller : null, null, ' oUhbaqbcaibvnWffWVcC', null); })(); (function() { var WMODEL_DATA = {}; WMODEL_DATA.name = "commentsSlider"; WMODEL_DATA.slot = null; WMODEL_DATA.OPTIONS = {"name":"commentsSlider"}; TH.installWidgetController('components.slider', 'componentsSlider8', WMODEL_DATA, typeof controller == 'function' ? controller : null, null, ' oUhballbvbdSaoUhM', null); })();